ASP.Net MVC 3 EditorFor CheckBoxList

I am developing an ASP.Net MVC 3 web application using Entity Framework 4.1. I'm having trouble displaying a CheckBoxList. Let me explain.

I have two ViewModel objects

public class ViewModelShiftSubSpecialties
{
    public IEnumerable<ViewModelCheckBox> SpecialtyList { get; set; }
}

public class ViewModelCheckBox
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
    public string Specialty { get; set; }
}

In my controller, I populate my ViewModels

        IList<RelationshipGradeSub> gradeSubSpecialties = GetSubSpecialtiesForGrade(firstShiftGrade.gradeID);

        ViewModelShiftSubSpecialties viewModel = new ViewModelShiftSubSpecialties();

        var checkBoxList = new List<ViewModelCheckBox>();

        foreach (var item in gradeSubSpecialties)
        {
            ViewModelCheckBox chkBox = new ViewModelCheckBox { Id = item.subID.ToString(), Name = item.ListSubSpecialty.description, Checked = false, Specialty=item.ListSubSpecialty.ListItemParent.description };
            checkBoxList.Add(chkBox);
        }

        viewModel.SpecialtyList = checkBoxList;

        return View(viewModel);

I also have a partial view that is used as an EditorTemplate to display my checkboxes

@Html.HiddenFor(x => x.Id)        
@Html.CheckBoxFor(x => x.Checked)
@Html.LabelFor(x => x.Name, Model.Name)<br />

Then, in my view, I try to filter through Model.Specialty and classify the elements into three checkboxes based on their Specialty, that is:

@foreach (var sub in Model.SpecialtyList)
{
if (sub.Specialty.Equals("Medicine"))
{
    @Html.EditorFor(m => m.SpecialtyList)
}
else if (sub.Specialty.Equals("Surgery"))
{
    @Html.EditorFor(m => m.SpecialtyList)
}
else if (sub.Specialty.Equals("Pathology"))
{
    @Html.EditorFor(m => m.SpecialtyList)
}

}

But that doesn't seem to work at all, that it gives me nothing in the 1st or 2nd checkbox, and then all the list items in the 3rd checkbox. Basically, what I'm trying to achieve is something like this

enter image description here

Can someone please give me some pointers to make this work?

Thank.

+3
1

, EditorTemplate ViewModelCheckBox:

@Html.HiddenFor(x => x.Id)        
@Html.CheckBoxFor(x => x.Checked)
@Html.LabelFor(x => x.Name, Model.Name)<br />

List<ViewModelCheckBox> @Html.EditorFor(m => m.SpecialtyList), , , - ViewModelCheckBox:

@Html.EditorFor(m => sub)

, .

+2

All Articles