这就是我的viewmodel的构建方式:
public IEnumerable<EducationItem> Educations { get; set; }
public class EducationItem
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public FlagModel[] Flags;
}
因此,对于每个教育项目,我都有一系列标志。
这是我的观点:
<table>
<thead>
<tr>
<th>ID</th>
<th>Education</th>
<th>Flags</th>
</tr>
</thead>
<tbody>
@{var educations = Model.Educations.ToArray();}
@for (int i = 0; i < educations.Length; i++)
{
@Html.HiddenFor(m => educations[i].Id)
<tr>
<td>@Html.ActionLink(educations[i].Id.ToString(), "Edit", "Education", new { id = educations[i].Id }, null)</td>
<td>@educations[i].Name</td>
<td>
<table>
@for (int j = 0; j < educations[i].Flags.Count(); j++)
{
var txtStartId = string.Format(@"txt_start_{0}", j);
var txtEndId = string.Format(@"txt_end_{0}", j);
var flag = educations[i].Flags.ElementAt(j);
<tr>
<td>
<input type="hidden" name="educations[@i].Flags[@j].Index" value="@j" />
@Html.CheckBoxFor(m => educations[i].Flags[j].Checked, new { id = string.Format("chk_flag_{0}", j) })
@(Html.GetCacheModel<Flag>(flag.Id).Name)
</td>
<td>
@Html.EditorFieldFor(m => educations[i].Flags[j].StartDate, "Start date", htmlAttributes: new { id = string.Format("txt_start_{0}", i) })
<input type="text" name="@string.Format("Flags[{0}].StartDate",j)"/>
</td>
<td>
@Html.EditorFieldFor(m => educations[i].Flags[j].EndDate, "End date", htmlAttributes: new { id = string.Format("txt_end_{0}", i) })
@Html.HiddenFor(m => educations[i].Flags[j].Id)
</td>
</tr>
}
</table>
</td>
</tr>
}
</tbody>
</table>
但是当我到达我的帖子,我接受我的viewmodel作为参数时,flags为null。
我无法弄清楚为什么值不会传递。(我已尝试使用常规文本框而不是编辑器字段,但结果相同)
任何帮助将不胜感激!