Asp.net mvc 4 在控制器上编辑IEnumerable null
我不能在这上面使用一个Asp.net mvc 4 在控制器上编辑IEnumerable null,asp.net-mvc-4,Asp.net Mvc 4,我不能在这上面使用一个列表,只有在没有编辑标签的视图上,我有一个视图工作正常,但我不能编辑其中的一个列表 @model IEnumerable<MyMixtapezServerCodeHomePage.Models.album> @using (Html.BeginForm("FeatureSystem", "album", FormMethod.Post)) { <table> <tr> <th>
列表
,只有在没有编辑标签的视图上,我有一个视图工作正常,但我不能编辑其中的一个列表
@model IEnumerable<MyMixtapezServerCodeHomePage.Models.album>
@using (Html.BeginForm("FeatureSystem", "album", FormMethod.Post))
{
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.name)
</th>
<th>
@Html.DisplayNameFor(model => model.feature)
</th>
<th>
@Html.DisplayNameFor(model => model.feature_order)
</th>
<th></th>
</tr>
@foreach (var item in @Model)
{
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => item.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.name,"album")
@Html.ValidationMessageFor(model => item.name)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => item.feature,"album")
</div>
<div class="editor-field">
@Html.EditorFor(model => item.feature)
@Html.ValidationMessageFor(model => item.feature)
</div>
</td>
<td>
<div class="editor-label">
@Html.LabelFor(model => item.feature_order)
</div>
<div class="editor-field">
@Html.EditorFor(model => item.feature_order,"album")
@Html.ValidationMessageFor(model => item.feature_order)
</div>
</td>
<td></td>
</tr>
}
<input type="submit" />
</table>}
@model IEnumerable
@使用(Html.BeginForm(“FeatureSystem”、“album”、FormMethod.Post))
{
@DisplayNameFor(model=>model.name)
@DisplayNameFor(model=>model.feature)
@Html.DisplayNameFor(model=>model.feature\u顺序)
@foreach(@Model中的var项)
{
@LabelFor(model=>item.name)
@EditorFor(model=>item.name,“相册”)
@Html.ValidationMessageFor(model=>item.name)
@LabelFor(model=>item.feature,“相册”)
@EditorFor(model=>item.feature)
@Html.ValidationMessageFor(model=>item.feature)
@LabelFor(model=>item.feature\u顺序)
@EditorFor(model=>item.feature\u顺序,“相册”)
@Html.ValidationMessageFor(模型=>item.feature\u顺序)
}
}
控制器
[HttpPost]
public ActionResult FeatureSystem(IEnumerable<album> albums)
{
foreach (album album in albums)
{
if (ModelState.IsValid)
{
albumRepository.Update(album);
albumRepository.SaveChanges();
}
}
return RedirectToAction("Index");
}
[HttpPost]
公共行动结果功能系统(IEnumerable相册)
{
foreach(相册中的相册)
{
if(ModelState.IsValid)
{
albumRepository.Update(相册);
albumRepository.SaveChanges();
}
}
返回操作(“索引”);
}
我收到的相册是空的,为什么?我可以编辑一个列表吗?我只需要这个。我相信自mvc3以来,这一点没有改变 所以要有一个可编辑的集合,必须使用for循环,而不是foreach,否则绑定将无法工作
@for(var i = 0; i < Model.Count; i++)
{
<tr>
<td>
<div class="editor-label">
@Html.LabelFor(model => Model[i].name)
</div>
<div class="editor-field">
@Html.EditorFor(model => Model[i].name,"album")
@Html.ValidationMessageFor(model => Model[i].name)
</div>
</td>
<td>
...
}
(变量i=0;i看看,旧的,但我认为还没有过时。在视图中使用IList而不是IEnumerable,并用for循环替换foreach循环 第1步: 使用 有关更多详细信息,请参阅
@model IList <MyMixtapezServerCodeHomePage.Models.album>
@model IEnumerable<MyMixtapezServerCodeHomePage.Models.album>
@for (var i = 0; i < Model.Count; i++)
@foreach (var item in @Model)