C# 列表中的列表(嵌套列表)未获取值
这就是我的viewmodel的构建方式:C# 列表中的列表(嵌套列表)未获取值,c#,model-view-controller,nested-lists,C#,Model View Controller,Nested Lists,这就是我的viewmodel的构建方式: public IEnumerable<EducationItem> Educations { get; set; } public class EducationItem { public int Id { get; set; } [Required] public string Name { get; set; } public FlagModel
public IEnumerable<EducationItem> Educations { get; set; }
public class EducationItem
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public FlagModel[] Flags;
}
公共IEnumerable教育{get;set;}
公共课教育项目
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共标志模型[]标志;
}
因此,对于每个教育项目,我都有一系列的标志
我的看法是:
<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>
身份证件
教育
旗帜
@{var educations=Model.educations.ToArray();}
@for(int i=0;ieducations[i].Id)
@ActionLink(教育[i].Id.ToString(),“编辑”,“教育”,新建{Id=educations[i].Id},null)
@教育[i].名称
@对于(int j=0;jeducations[i].Flags[j].Checked,new{id=string.Format(“chk_flag{0}”,j)})
@(Html.GetCacheModel(flag.Id).Name)
@EditorFieldFor(m=>educations[i].Flags[j].StartDate,“开始日期”,htmlAttributes:new{id=string.Format(“txt_开始{0}”,i)})
@EditorFieldFor(m=>educations[i]。标志[j]。结束日期,“结束日期”,htmlAttributes:new{id=string.Format(“txt_End{0}”,i)})
@Html.HiddenFor(m=>educations[i].Flags[j].Id)
}
}
但当我在帖子中接受viewmodel作为参数时,flags为null
我无法解释为什么不传递这些值。(我尝试使用常规文本框,而不是editorfieldfor,结果相同)
任何帮助都将不胜感激 加载viewmodel时,是否获得标志的任何值?是否可以发布
教育
元素的呈现HTML?@qamar,是的,如果存在,则正确加载值。在for循环中,请尝试使用“更大”或“相等”(这是教育元素呈现部分的粘贴箱: