Generating an MVC3 RadioButton list in a loop statement

和自甴很熟 提交于 2019-12-02 19:19:06

问题


A collegaue of mine created a model and here it is.

Model

[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; }
}

I intend to create a loop that will generate the radiobutton list for the ApplicationStatusId , something like this...

Razor Code

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

    }

but what happens is that ALL radiobhuttons created has the same name so they are grouped as one giant radiobutton collection. not via the schoolID... scratches head

Can someone help me here and point me to the right direction on how i will be able to create radio buttons that are grouped per row?


回答1:


I would do two things.

First up, I would remove the filtering logic from the view. What I mean is this part:

Model.CollegeList.Where(m => m.CategoryId == 3).OrderBy(m => m.SchoolName).ToList<SchoolModel>()

That sort of logic belongs in a service. Also it will make the view much cleaner.

Secondly, I think you'll need to use a for-loop so MVC binds everything back how you want:

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

You'll notice after using the for-loop, that the radiobutton names and ID's also contain their index in the CollegeList. For example:

<input id="CollegeList_0__SchoolId" name="CollegeList[0].SchoolId" type="radio" value="2">


来源:https://stackoverflow.com/questions/11839111/generating-an-mvc3-radiobutton-list-in-a-loop-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!