【问题标题】:MVC pass data from not strongly typed view to controllerMVC 将数据从非强类型视图传递到控制器
【发布时间】:2016-07-20 06:22:08
【问题描述】:

我正在尝试学习 MVC,并且已经坚持了很长时间,所有在线教程都使用强类型视图,但我的视图不是强类型,

@using (Html.BeginForm("addInventory", "AdminController", FormMethod.Post))
{
    <div class="form-style-2-heading">Add New Inventory</div>
    <label>
        <span>No <span class="required">*</span></span>@Html.TextBox("no", null, new { id = "no", Class = "input-field" })
    </label>
    <label>
        <span>Name <span class="required">*</span></span>@Html.TextBox("name", null, new { id = "name", Class = "input-field" })
    </label>
    <label>
        <span>Primary Type <span class="required">*</span></span>@Html.DropDownList("typeList", ViewBag.typeList as SelectList, new { id = "primarytype", Class = "select-field" })
    </label>
    <label>
        <span>Secondary Type <span class="required">*</span></span>@Html.DropDownList("typeList", ViewBag.typeList as SelectList, new { id = "secondarytype", Class = "select-field"})
    </label>

    <label><span>&nbsp;</span><input type="submit" value="Submit" /></label>
}

所以我成功地将我的下拉列表与来自控制器的数据绑定了,但我似乎无法反其道而行之

编辑:

型号:

public class inventoryModel
{
    public int no { get; set; }
    public string name { get; set; }
    public int primaryType { get; set; }
    public int secondaryType { get; set; }
}

控制器:

private ActionResult addInventory()
{

   return View();
}

【问题讨论】:

  • 你的问题是什么?
  • 你没有强绑定任何东西。显示您的模型和 POST 方法的签名
  • @teovankot 嗨,问题是,我想知道单击按钮后如何从控制器端获取 html 控件的数据
  • 您使用name="typeList" 创建了2 个下拉菜单,而您的模型甚至不包含名为typeList 的属性。如果您想绑定到您的模型,请使用 @Html.TextBoxFor(m =&gt; m.no)name 同上)和下拉列表 @Html.DropDownListFor(m =&gt; m.primaryType, Model.PrimaryTypeList) 并将属性 IEnumerable&lt;SelectListItem&gt; PrimaryTypeList 添加到您的模型(secondaryType 同上)
  • @StephenMuecke 哦,下拉列表已经与我的数据库中的数据绑定,我需要知道如何获取 selectedValue 等

标签: c# asp.net asp.net-mvc


【解决方案1】:

你的错误在这里:

<label>
    <span>Primary Type <span class="required">*</span></span>
    @Html.DropDownList("primaryType", (IEnumerable<SelectListItem>)ViewBag.typeList, new { @class = "select-field" })
</label>
<label>
    <span>Secondary Type <span class="required">*</span></span>
    @Html.DropDownList("secondaryType", (IEnumerable<SelectListItem>)ViewBag.typeList, new { @class = "select-field"})
</label>

不要在 htmlAttributes 中使用 id 第一个辅助参数用于标签 idname on POST 标签的值将通过 name 绑定到您的模型属性。

还请注意,我使用 @ 符号转义了 class 名称。

在此处发布您的控制器签名应该是什么:

[HttpPost]
private ActionResult addInventory(inventoryModel model)
{
   var data = model.secondaryType; //here you can get your posted data
   return View();
}

【讨论】:

  • 我收到此错误:具有键“primaryType”的 ViewData 项的类型为“System.Int32”,但必须为“IEnumerable”类型。当我尝试它时,它是否传递了选择列表而不是选定的值?
  • 使用IEnumerable&lt;SelectListItem&gt; 比使用SelectList 更容易,只需将您的typeList 更改为IEnumerable&lt;SelectListItem&gt; 我更新答案
  • @BloopieBloops - 阅读我在问题中给你的链接 - 这意味着 ViewBag.typeListnull
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多