Click here to Skip to main content
15,881,856 members
Articles / Web Development
Tip/Trick

How to select an initial value for a DropDownList in MVC3 App

Rate me:
Please Sign up or sign in to vote.
3.40/5 (4 votes)
6 Jan 2012CPOL 77.1K   2   8
How to select an initial value for a DropDownList in MVC3 App
In MVC3/.NET4.0/VS2010 web application, selecting a default value for an extension method - DropDownList or DropDownListFor is tricky.

The DropDownList first parameter should be any valid name other than the list instance name.
For example, you can have DropDownList("xyz",...) not DropDownList("CompaniesRef",...), for the below example, when Model.CompaniesRef instance is used to populate the list.

Same rule applies to DropDownListFor() method too. For DropDownListFor(m=>m.Name,..), make sure the "m.Name" (actually Name) is any public "string" property in Model class.

See the example below:

PersonView.cshtml
HTML
  <table>
@model MyTestApp.Models.PersonAddressModel
  <tr>
   <!-- DONOT SELECT "HP" FROM THE LIST, INSTEAD SELECTS THE FIRST ITEM-->
   <td>@Html.DropDownList("CompaniesRef", new SelectList(
            Model.CompaniesRef,"Key","Value","HP"))
   </td>
   <td>@Html.DropDownListFor(m=>m.CompaniesRef, new SelectList(
            Model.CompaniesRef,"Key","Value","HP"))
   </td>
 
 <!-- SELECTs "HP" FROM THE LIST. THIS IS CORRECT BEHAVIOR -->
  <td>@Html.DropDownList("CompaniesRef_", new SelectList(
            Model.CompaniesRef,"Key","Value","HP"))
   </td>
   <td>@Html.DropDownListFor(m=>m.Name, new SelectList(
            Model.CompaniesRef,"Key","Value","HP"))
   </td>
   </tr>
  </table>


Model class
C#
class Person
{
  public KeyValuePair<string, string>[] CompaniesRef { get; set; } 
  public string Name {get;set;}
  public void Load()
  {
     CompaniesRef = new KeyValuePair<string, string>[]
     {
      new KeyValuePair<string, string>("MSFT", "Microsoft"),
      new KeyValuePair<string, string>("IBM", "IBM"),
      new KeyValuePair<string, string>("HP", "HP"),
      new KeyValuePair<string, string>("GOOG", "Google"),
      new KeyValuePair<string, string>("BOA", "BANK OF AMERICA"),
      new KeyValuePair<string, string>("AMC", "American Airlines")
     };
  }
} 


Controller method
C#
public class PersonController : Controller
 {
  public ActionResult Person(Person p)
   {
    p.Load();
    return View("PersonView",p);   
   }
 }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer MicroLink LLC
United States United States
Started as C++ developer later fell in love with .NET and moved to .NET. I still use Notepad to type in C# console apps. for learning unknown areas. The software business areas I worked are diversified - Gas, Truck Leasing, Telecom, many State and Federal projects.

Comments and Discussions

 
GeneralMy vote of 5 Pin
ernie_hudds21-Sep-17 0:50
ernie_hudds21-Sep-17 0:50 
QuestionGood article Pin
manoj.cm29-Sep-15 22:49
manoj.cm29-Sep-15 22:49 
GeneralMy vote of 1 Pin
AbdullaMohammad1-Oct-14 0:33
AbdullaMohammad1-Oct-14 0:33 
QuestionThanx Pin
ankita_singh903-Feb-14 19:00
ankita_singh903-Feb-14 19:00 
GeneralReason for my vote of 1 Model class is awfull from an archit... Pin
Steve Mihy6-Jan-12 4:51
Steve Mihy6-Jan-12 4:51 
GeneralRe: I'm trying to show how to set up an initial value to a DropD... Pin
Kethu Sasikanth6-Jan-12 12:05
Kethu Sasikanth6-Jan-12 12:05 
I'm trying to show how to set up an initial value to a DropDownList control in MVC with a simplest example. I have no where, above, talked about the architecture or the best practices.
GeneralRe: I'm trying to show how to set up an initial value to a DropD... Pin
Member 233417217-May-14 1:22
Member 233417217-May-14 1:22 
GeneralRe: Reason for my vote of 1Model class is awfull from an archit... Pin
Member 233417217-May-14 1:23
Member 233417217-May-14 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.