MVC多个DropDownListFor [英] MVC multiple DropDownListFor
问题描述
我创建的项目列表的表视图,我有每一行的DropDownListFor从另一个列表中选择值。我要地图的每个名称与对应的code。如果某些名称被媒体链接映射我怎么能显示一些DropdownLists与选定的价值?
谢谢。
I created a table view from a list of items and I have in each row a DropDownListFor to select values from another list. I want to map each Name with a corresponding Code. If some Names are allready mapped how can I display some DropdownLists with the selected value? Thank you.
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Code)
</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(x => item.Name)
</td>
<td>
@Html.DropDownListFor(x => item.Code, (SelectList)ViewBag.SelectCodes)
</td>
</tr>
}
推荐答案
首先,如果这个观点是你提交到一个控制器方法,那么你就需要使用为$ C形式的一部分$ C>环或
属性是无效的HTML。 EditorTemplate
为您的模型。你的的foreach
循环产生重复的名称
属性,其中有你的模型及其也产生重复的<$ C $没有关系C> ID
Firstly if this view is part of a form your submitting to a controller method, then you need to use a for
loop or EditorTemplate
for your model. Your foreach
loop is generating duplicate name
attributes which have no relationship to your model and its also generating duplicate id
attributes which is invalid html.
不幸的是 @ Html.DropDownListFor()
行为有点不同于其他助手在一个循环中渲染控制何时以及需要产生一个新的的SelectList
在每次迭代中,并设置选
属性。使用为
循环您的观点必须(模型必须的IList&LT; T&GT;
)
Unfortunately @Html.DropDownListFor()
behaves a little differently than other helpers when rendering controls in a loop and its necessary to generate a new SelectList
in each iteration and set the Selected
property. Using a for
loop your view needs to be (the model must be IList<T>
)
@for(int i = 0; i < Model.Count; i++)
{
@Html.DropDownListFor(m => m[i].Code, new SelectList(ViewBag.SelectCodes, "Value", "Text", Model[i].Code))
}
请注意,这是基于 ViewBag.Select codeS
已经是一个的SelectList
,这是不是真的必要。这可能是一些像新的SelectList(ViewBag.Select codeS,ID,姓名,模型[1]。code)
,其中选择codeS
是包含属性的对象的集合 ID
和名称
。
Note this is based on ViewBag.SelectCodes
already being a SelectList
, which is not really necessary. It could be something like new SelectList(ViewBag.SelectCodes, "ID", "Name", Model[i].Code)
where SelectCodes
is a collection of objects containing properties ID
and Name
.
一个更好的选择是使用自定义的 EditorTemplate
为您的模型。假设你的模式是 MyModel.cs
,然后创建 /Views/Shared/EditorTemplates/MyModel.cshtml
的局部视图(模板的名称必须与类的名称相匹配)
A better alternative is to use a custom EditorTemplate
for your model. Assuming your model is MyModel.cs
, then create a partial view in /Views/Shared/EditorTemplates/MyModel.cshtml
(the name of the template must match the name of the class)
@model MyModel
<tr>
<td>@Html.DisplayFor(m => m.Name)</td>
<td>@Html.DropDownListFor(m => m.Code, (SelectList)ViewData["SelectCodes"])</td>
</tr>
然后在主视图
@model IEnumerable<MyModel>
....
<table>
<thead>
....
</thead>
<tbody>
@Html.EditorFor(m => m, new { SelectCodes = ViewBag.SelectCodes })
</tbody>
</table>
的 EditorFor()
方法传递的SelectList
来使用模板 AdditionalViewData
和方法将正确生成每个项目的HTML集合中,而不需要产生新的。
The EditorFor()
method is passing the SelectList
to the template using AdditionalViewData
and the method will correctly generate the html for each item in the collection without have to generate new SelectList
's.
这篇关于MVC多个DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!