Altaf Khatri
Altaf Khatri

RSS feed for website. Twitter account of website.
∗Recently Added - How to take care of Betta fighter fish and increase life span?  10/30/2013

Find me on web




How to bind IList with MVC Dropdownlist box?

I am a big fan of IList, a System.Collections.Generic interface.
It is convenient to use IList in many situations, when the object needs to be iterated without knowing the length, type of the repeating object using foreach loop and a var datatype.
IList inherits IEnumerable interface and hence it inherits all the good things associated with it.

To associate IList with the MVC dropdownlist is simple. I will discuss 2 methods:

Method 1:
Perform the following actions in the MVC Controller

  1. Create IList of SelectListItem type.
    IList<SelectListItem> iliSLI = new List<SelectListItem>();
  2. Create an object of type SelectListItem and assign the text and value properties;
    SelectListItem sli = new SelectListItem();
    sli.Text = "What is your Mother's maiden name?";
    sli.Value = "1";
    sli.Selected = true;
  3. Add the object created above to the IList collection. You can loop through the list of items retrieved from the database and add the object to the IList collection.
  4. Create as many item as you want to appear in the dropdownlist box following the steps above.
  5. Assign the IList to the ViewData with the same name as the dropdownlist box. In this case "SecretQuestion"
    ViewData["SecretQuestion"] = iliSLI;

In the View of the page use the following MVC Html helper:

<%= Html.DropDownList("SecretQuestion")%>

Method 2:

  1. Here we will be using the database to retrieve data using the stored procedure. I am getting data from the stored procedure named: udp_GetSecretQuestionlist. This stored procedure returns me the following columns:
    1. pkSecretQuestion
    2. SecretQuestionDetail
    3. dtCreated
    4. CreatedBy
    5. UpdatedOn
    6. UpdatedBy
    For the discussion at hand, if we want to bind the dropdownlist with the SecretQuestionDetail as the text field and the pkSecretQuestion as the value field, then it can be achieved by using the below lines of code in Controller:
    IList<tbl_SecretQuestion> iltblSecretQuestions = null;
    using (TaskManagerEntities tme = new TaskManagerEntities())
       iltblSecretQuestions = tme.udp_GetSecretQuestionlist().ToList();
    where TaskManagerEntities is my DataEntity Framework's class.
    and tbl_SecretQuestion is the entity class associated as the output of the stored procedure.
  2. Now selecting the required columns for the binding with the MVC Dropdownlist
    ViewData["SecretQuestion"] = new SelectList(iltblSecretQuestions, "pkSecretQuestion", "SecretQuestionDetail", "2");
    where textField = SecretQuestionDetail
    valueField = pkSecretQuestion and
    the last parameter 2 represents the Question for which the pkSecretQuestion = 2 and not the second item in the list.
  3. In the View of the page use the following MVC Html helper:
    <%= Html.DropDownList("SecretQuestion")%>
This is it. Isn't it simple?

SiteMap  |  Contact Me