How to bind data to MultiSelectList in MVC4 application?

In this resource, I have added code for selecting more than one items in Html.ListBoxFor control. I am using Entity Framework i.e. .edmx file as my model. Binding data in ASP.NET MVC application is as simple as binding a normal drop-down list.

Description- When to use multiselect list?
Whenever we want to select more than one value present in the listbox, we use multiselect list.
Steps: 1) Create class clsData and declare get and set properties for dataid as interger and dataname as string.

public class clsData
public int dataid { get; set; }
public string dataname { get; set; }

2) In the controller, you have to write following:

List<clsData> obj = new List<clsData>();

obj.Add(new clsData { dataid = 1, dataname = 'System information'});

obj.Add(new clsData { dataid = 2, dataname = 'Product information'});

Similarly you can add multiple data which will be added in the list and display to end user after running the application.
3) Create object of MultiSelectList which is msldata.

MultiSelectList msldata = new MultiSelectList(obj, "dataid", "dataname", "");

This way you can bind data to MultiSelectList.
4) In Razor view , you have use following:

@Html.ListBoxFor(model => Model.propertynameforselectedid, (MultiSelectList)Model.propertyname)


Guest Author: Aaron03 Oct 2013

Thank you, this is the first post about this that made any sense. Worked the first time!

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: