Click here to Skip to main content
12,253,599 members (54,412 online)
Click here to Skip to main content
Add your own
alternative version

Stats

53.9K views
2 bookmarked
Posted

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

, 6 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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
  <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
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
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)

Share

About the Author

Kethu Sasikanth
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.

You may also be interested in...

Comments and Discussions

 
QuestionGood article Pin
manoj.cm29-Sep-15 22:49
membermanoj.cm29-Sep-15 22:49 
GeneralMy vote of 1 Pin
AbdullaMohammad1-Oct-14 0:33
memberAbdullaMohammad1-Oct-14 0:33 
QuestionThanx Pin
ankita_singh903-Feb-14 19:00
memberankita_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
memberSteve 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
memberKethu Sasikanth6-Jan-12 12:05 
GeneralRe: I'm trying to show how to set up an initial value to a DropD... Pin
Member 233417217-May-14 1:22
memberMember 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
memberMember 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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 6 Jan 2012
Article Copyright 2012 by Kethu Sasikanth
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid