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>

    }

しかし、作成されたすべてのラジオボタンは同じ名前であるため、1 つの巨大なラジオボタン コレクションとしてグループ化されます。schoolID経由ではありません...頭を悩ませます

誰かがここで私を助けて、行ごとにグループ化されたラジオボタンを作成する方法について正しい方向に向けることができますか?

4

1 に答える 1

1

私は2つのことをします。

まず、フィルター ロジックをビューから削除します。私が言いたいのは、この部分です:

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 に答える