0

我的一位同事创建了一个模型,就在这里。

模型

[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 生成单选按钮列表,就像这样......

剃刀代码

   @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>

    }

但是会发生的是,所有创建的单选按钮都具有相同的名称,因此它们被分组为一个巨大的单选按钮集合。不是通过学校ID...挠头

有人可以在这里帮助我,并指出我将如何创建按行分组的单选按钮的正确方向吗?

4

1 回答 1

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 循环后,您会注意到单选按钮名称和 ID 还包含它们在 CollegeList 中的索引。例如:

<input id="CollegeList_0__SchoolId" name="CollegeList[0].SchoolId" type="radio" value="2">
于 2012-08-07T04:23:22.290 回答