与えられたモデル
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
。そこに追加のドットがあるため、投稿時にモデルに正しくバインドされていません。どうすればこれを修正できますか?
リストへのモデルのバインドに従って、IDが何であるかを確認できますが、正しい構文を理解できません。
IEnumerable
また、?を使用してこれを達成するためのよりエレガントな方法はあり@foreach
ますか?
関連している: