[英]Send Dropdown Selected Value from View to Controller using "DropDownListFor" in MVC
[英]MVC 4 table using DisplayFor and dropdown using DropDownListFor in same view
我在视图中需要一个下拉列表和一个表格。
这是我的下拉列表:
<td>
@Html.DropDownListFor(model => model.MP_Name, (IEnumerable<SelectListItem>) enter code hereViewbag.vbNameMP)
</td>
我想使用Html.DisplayFor引用表项:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserCode)
</td>
看来我需要用于表的@model IEnumerable <,但是在下拉列表上却出现错误。 如果我从@model语句中删除IEnumerable,则下拉列表将起作用,但是表DisplayFor会出现错误。 如何容纳下拉列表和表格?
使用IList<T>
并在您的for
循环中容纳下拉列表和displayfor:
@model IList<MyViewModel>
<table>
@for (var i = 0; i < Model.Count; i++)
{
<tr>
<td>
@Html.DropDownListFor(
x => x[i].MP_Name,
(IEnumerable<SelectListItem>)Viewbag.vbNameMP
)
</td>
<td>
@Html.DisplayFor(x => x[i].UserCode)
</td>
</tr>
}
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.