【发布时间】: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> </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 => m.no)(name同上)和下拉列表@Html.DropDownListFor(m => m.primaryType, Model.PrimaryTypeList)并将属性IEnumerable<SelectListItem> PrimaryTypeList添加到您的模型(secondaryType同上) -
@StephenMuecke 哦,下拉列表已经与我的数据库中的数据绑定,我需要知道如何获取 selectedValue 等
标签: c# asp.net asp.net-mvc