Generating an MVC3 RadioButton list in a loop statement

前端 未结 1 1057
情话喂你
情话喂你 2021-01-28 02:12

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

Model

[Serializable]
public class ModifyCollegeListModel
{
    public List

        
相关标签:
1条回答
  • 2021-01-28 03:00

    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">
    
    0 讨论(0)
提交回复
热议问题