我的一个同事创建了一个模型,现在就是。
模型
[Serializable]
public class ModifyCollegeListModel
{
public List<SchoolModel> CollegeList { get; set; }
public List<SchoolListModel> SchoolList { get; set; }
public string Notes { get; set; }
public int QuestionnaireId { get; set; }
}
[Serializable]
public class SchoolModel
{
public Guid SchoolId { get; set; }
public string SchoolName { get; set; }
public string StateName { get; set; }
public int DisplayIndex { get; set; }
public int DetailId { get; set; }
public int CategoryId { get; set; }
public int? ApplicationStatusId { get; set; }
}
我打算创建一个循环来生成ApplicationStatusId的radiobutton列表,就像这样......
剃刀代码
@foreach (SchoolModel justright in Model.CollegeList.Where(m => m.CategoryId == 3).OrderBy(m => m.SchoolName).ToList<SchoolModel>())
{
<tr class="@HtmlHelpers.WriteIf(eventCounter % 2 == 0, "even", "odd")">
<td class="school"><b>@justright.SchoolName</b></td>
<td class="location"><b>@justright.StateName</b></td>
<td><label>@Html.RadioButtonFor(x => justright.SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.DidNotApply)</label></td>
<td><label>@Html.RadioButtonFor(x => justright.SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.Accepted)</label></td>
<td><label>@Html.RadioButtonFor(x => justright.SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.NotAccepted)</label></td>
</tr>
}
但是,所发生的是所有创建的radiobhutton具有相同的名称,因此它们被分组为一个巨大的radiobutton集合。不是通过schoolID ... 划伤头
有人可以帮助我,并指出我如何能够创建按行分组的单选按钮的正确方向吗?
答案 0 :(得分:1)
我会做两件事。
首先,我会从视图中删除过滤逻辑。我的意思是这部分:
Model.CollegeList.Where(m => m.CategoryId == 3).OrderBy(m => m.SchoolName).ToList<SchoolModel>()
这种逻辑属于服务。它也会使视图更清晰。
其次,我认为您需要使用for循环,因此MVC会根据您的需要将所有内容绑定:
for (int i = 0; i < Model.CollegeList.Count; i++) {
<tr class="@HtmlHelpers.WriteIf(eventCounter % 2 == 0, "even", "odd")">
<td class="school"><b>@CollegeList[i].SchoolName</b></td>
<td class="location"><b>@CollegeList[i].StateName</b></td>
<td><label>@Html.RadioButtonFor(x => x.CollegeList[i].SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.DidNotApply)</label></td>
<td><label>@Html.RadioButtonFor(x => x.CollegeList[i].SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.Accepted)</label></td>
<td><label>@Html.RadioButtonFor(x => x.CollegeList[i].SchoolId, (int)BrightHorizons.CC.BusinessLogic.CollegeListApplicationStatusEnum.NotAccepted)</label></td>
</tr>
}
使用for循环后你会注意到,radiobutton名称和ID也包含在CollegeList中的索引。例如:
<input id="CollegeList_0__SchoolId" name="CollegeList[0].SchoolId" type="radio" value="2">