【发布时间】:2020-03-19 22:01:14
【问题描述】:
这是 .NET MVC 项目的一部分。
我有一个 DropDownList,它由我的模型中的字符串数组填充。 The actual list functions fine and populates correctly, but when an item is selected, there is no value passed.该值应与从下拉列表中选择的文本相同。
我可以看到它可能没有分配到任何地方,但我对 MVC/HTML 项目相对缺乏经验。
<div class="form-group">
@Html.LabelFor(model => model.Cores, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Cores", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Cores, "", new { @class = "text-danger" })
</div>
</div>
我正在使用的代码是从我(显然)接管的一个更大的项目中修改而来的,所以我正在学习这个。
编辑:
这就是核心列表被初始化并传递到视图页面的方式。
var cores = db.Cores.ToList();
var coreName = new List<string>();
foreach (Core core in cores)
{
coreName.Add(core.Name);
}
ViewBag.Cores = new SelectList(coreName);
return View();
【问题讨论】:
-
这可能与“获取 DropDownList.Asp.NET MVC 的选定值”stackoverflow.com/questions/15881575/… 重复
-
控制器动作签名是什么?在浏览器的调试工具中,生成的
<select>的name是什么?在那些相同的工具中,值是否在 HTTP 请求中传递?这完全是表格的一部分吗?我们需要检查一个更大的图景。
标签: html asp.net-mvc