使用dropdownlistfor在Asp.Net MVC视图的foreach? [英] Use dropdownlistfor with foreach in Asp.Net MVC View?
问题描述
我有一个foreach循环的模型的列表属性视图。现在,我希望能够让用户设置的每个使用一个DropDownList列表中的项的值。但我不知道该怎么做。我用这样的事情时,它不是一个foreach循环:
@ Html.DropDownListFor(型号=> model.Level,新的SelectList(新[] {1,2,3,4,5},Model.Level))
但我怎么做到这一点时,我需要引用item.Level在循环呢?这里是我查看code:
< DIV ID =formDiv>
@using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ID =myForm会}))
{
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>尹恩惠arbetserfarenhet< /传说>
<表>
&所述; TR>
@ *<第i< /第i * @
<第i个
程序
< /第i
<第i个
NIVA
< /第i
< / TR>
@foreach(以型号VAR项){
&所述; TR>
&所述; TD>
@ item.Program.Name
< / TD>
&所述; TD> @ item.Level
< / TD>
< / TR>
}
< /表>
< /字段集>
}
< / DIV>
我有一个foreach循环的模式的列表属性。
视图
块引用>我会建议您避免赞成编辑模板的意见写循环。所以:
@model IEnumerable的< AppName.Models.ModelName>
< DIV ID =formDiv>
@using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ID =myForm会}))
{
@ Html.ValidationSummary(真)
<&字段集GT;
<传奇>尹恩惠arbetserfarenhet< /传说>
<表>
&所述; TR>
<第i个
程序
< /第i
<第i个
NIVA
< /第i
< / TR>
@ Html.EditorForModel()
< /表>
< /字段集>
}
< / DIV>和在相应的编辑模板(
〜/查看/共享/ EditorTemplate / ModelName.cshtml
)@model AppName.Models.ModelName
&所述; TR>
< TD> @ Model.Program.Name< / TD>
&所述; TD>
@ Html.DropDownListFor(
模型=> model.Level,
新的SelectList(
Enumerable.Range(1,5)。选择(X =>新建{值= X,文本= X}),
值,
文本
)
)
< / TD>
< / TR>因此,编辑模板将呈现为在模型中的每个元素(这是一些类型的集合)。最重要的部分就是编辑模板必须位于
〜/查看/共享/ EditorTemplates
并命名为XXX.cshtml
其中,XXX
是你的主视图模型集合中使用的类型名称。I have a View with a foreach loop for a list property of the model. Now, I want to be able to let the user set the value of each of the items in the list using a dropdownlist. But I'm not sure how to do that. I've used something like this when it is not in a foreach loop:
@Html.DropDownListFor(model => model.Level, new SelectList(new[] { 1, 2, 3, 4, 5 }, Model.Level))
But how do I do this when I need to reference item.Level in the loop instead? Here's my View code:
<div id="formDiv"> @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) { @Html.ValidationSummary(true) <fieldset> <legend>Ny arbetserfarenhet</legend> <table> <tr> @*<th></th>*@ <th> Program </th> <th> Nivå </th> </tr> @foreach (var item in Model) { <tr> <td> @item.Program.Name </td> <td> @item.Level </td> </tr> } </table> </fieldset> } </div>
解决方案I have a View with a foreach loop for a list property of the mode
I would recommend you to avoid writing loops in your views in favor of editor templates. So:
@model IEnumerable<AppName.Models.ModelName> <div id="formDiv"> @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm" })) { @Html.ValidationSummary(true) <fieldset> <legend>Ny arbetserfarenhet</legend> <table> <tr> <th> Program </th> <th> Nivå </th> </tr> @Html.EditorForModel() </table> </fieldset> } </div>
and in the corresponding editor template (
~/Views/Shared/EditorTemplate/ModelName.cshtml
):@model AppName.Models.ModelName <tr> <td>@Model.Program.Name</td> <td> @Html.DropDownListFor( model => model.Level, new SelectList( Enumerable.Range(1, 5).Select(x => new { Value = x, Text = x }), "Value", "Text" ) ) </td> </tr>
So the editor template will be rendered for each element in your model (which is a collection of some type). The important part is that the editor template must be located in
~/Views/Shared/EditorTemplates
and namedXXX.cshtml
whereXXX
is the type name used in your main view model collection.这篇关于使用dropdownlistfor在Asp.Net MVC视图的foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!