Given a Model
public class Task
{
public int TaskId { get; set; }
public string Title { get; set; }
public ICollection<SomeData> Information { get; set; }
}
where
public class SomeData
{
public int SomeDataId { get; set; }
public string Description { get; set; }
}
I have a view
@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>
and my partial is
@model IList<myProject.Models.SomeData>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.EditorFor(modelItem => Model[i].Description)
</td>
</tr>
}
However
My Html fields are being rendered like
<input class="text-box single-line" id="Information__0__Description" name="Information.[0].Description" type="text">
Where the names should be Information[0].Description
. It's got an additional dot in there, so is not being bound back to the model correctly when posted. How can I fix this?
As per Model binding to a list I can see what my Id's are supposed to be, but I just can't figure out the correct syntax.
Also, is there a more elegant way to achieve this with an IEnumerable
using a @foreach
?
Related:
ASP.Net MVC4 bind a "create view" to a model that contains List
ASP.NET MVC model binding an IList<> parameter
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…