【发布时间】:2013-02-07 12:18:22
【问题描述】:
给定一个模型
public class Task
{
public int TaskId { get; set; }
public string Title { get; set; }
public ICollection<SomeData> Information { get; set; }
}
在哪里
public class SomeData
{
public int SomeDataId { get; set; }
public string Description { get; set; }
}
我有意见
@model myProject.Models.Task
<div>
@Html.LabelFor(model => model.Title)
</div>
<table>
@Html.Partial("_InformationEdit", Model.Information.ToList(), new ViewDataDictionary(Html.ViewDataContainer.ViewData) {
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Information" }})
</table>
我的部分是
@model IList<myProject.Models.SomeData>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.EditorFor(modelItem => Model[i].Description)
</td>
</tr>
}
但是
我的 Html 字段被渲染为
<input class="text-box single-line" id="Information__0__Description" name="Information.[0].Description" type="text">
名称应该是Information[0].Description。它在那里有一个额外的点,因此在发布时没有正确绑定回模型。我该如何解决这个问题?
根据 Model binding to a list,我可以看到我的 ID 应该是什么,但我就是想不出正确的语法。
另外,有没有更优雅的方法可以通过IEnumerable 使用@foreach 来实现这一点?
相关:
ASP.Net MVC4 bind a "create view" to a model that contains List
【问题讨论】:
标签: c# asp.net razor asp.net-mvc-4 model-binding