4

I am developing an ASP.Net MVC 3 Web application and I recently posted a question about how to display a checkboxlist

ASP.Net MVC 3 Retrieve Checkbox List Values

and thanks to the help from RubbleFord and Darin Dimitrov I was able to get this working.

This works nicely for one checkboxlist, however, I now need to be able to display several checkboxlists on the same View, ie, see image attached.

enter image description here

The ViewModels I use to currently display one list are as follows;

public class ViewModelShiftSubSpecialties
{

    public ListItem specialtyName { get; set; }
    public IEnumerable<ViewModelCheckBox> SubSpecialityList { get; set; }

}

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

And within my Controller I populate ViewModelShiftSubSpecialties like so:

var subSpecialties = GetSubSpecialtiesForSpecialty(5);

        ViewModelShiftSubSpecialties viewModel = new ViewModelShiftSubSpecialties();

        var checkBoxList = new List<ViewModelCheckBox>();

        viewModel.specialtyName = _listService.GetListItemByID(5); //Medicine Specialty

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

        viewModel.SubSpecialityList = checkBoxList;

In my View, I display the list name and also use an Editor template to display the checkboxlist

<h3>@Model.specialtyName.description</h3>
@Html.EditorFor(m => m.SubSpecialityList)

However, I am totally stumped as how to get the above code to work with multiple checkboxlists on one View. Is this even possible?

I would really appreciate if someone could please help me with this.

Thanks.

1 Answer 1

5

It looks like you've done all the work already. You already have an Editor Template that works correctly with a ViewModelCheckBox IEnumerable. Editor templates wouldn't be useful if you couldn't reuse them for the same datatype. You just need to use it three times. Just extend your ViewModel

public class ViewModelShiftSubSpecialties
{
    public ListItem specialtyName { get; set; } //Might need 3 of these
    public IEnumerable<ViewModelCheckBox> SubSpecialityList1 { get; set; }
    public IEnumerable<ViewModelCheckBox> SubSpecialityList2 { get; set; }
    public IEnumerable<ViewModelCheckBox> SubSpecialityList3 { get; set; }
}

Create all three in your controller (and give them better names then I did).

And then in your View

@Html.EditorFor(m => m.SubSpecialityList1)
@Html.EditorFor(m => m.SubSpecialityList2)
@Html.EditorFor(m => m.SubSpecialityList3)

Alternatively you could create a class that contains a single specialty name and IEnumerable ViewModelCheckBox, and have your ViewModel have an IEnumerable of this new class. Then create a new Editor Template for this new class. I think this is worth it if your list size is variable/ might change. Otherwise I'd use the earlier solution for a simple fix.

public class ViewModelShiftSubSpecialties
{
    public class IEnumerable<SubSpecialty> { get; set; }
}

public class SubSpecialty
{
    public ListItem specialtyName { get; set; }
    public IEnumerable<ViewModelCheckBox> SubSpecialityList
}
Sign up to request clarification or add additional context in comments.

2 Comments

This looks good, thanks. Could you elaborate a bit more of your 2nd solution? Thanks.
Thanks for this AFinkelstein - this is really useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.