Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Anyone could explain me how to set selctedvalue for a dropdownlist in MVC 4. Below is my dropdownlist.

C#
@Html.DropDownListFor(model => model.Role,
               new List<SelectListItem>
               {
                   new SelectListItem { Text = "Engineer", Value = "1"},
                   new SelectListItem { Text = "Librarian", Value = "2"},
                   new SelectListItem { Text = "Admin", Value = "3"}
               }
              )


This is my edit page, i need to default the selected role when page got opened.
Thanks in advance.

Vijay
Posted
Updated 8-May-13 19:29pm
v2

Use SelectList to bind @HtmlDropdownListFor and specify selectedValue parameter in it.

http://msdn.microsoft.com/en-us/library/dd492553(v=vs.108).aspx[^]


VB
public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    Object selectedValue
)
 
Share this answer
 
Comments
[no name] 9-Jan-14 8:04am    
not an answer...
@Html.DropDownListFor(m => m.Education,
new List;SelectListItem>
{
new SelectListItem { Text = "B.Sc", Value = "B.Sc"},
new SelectListItem { Text = "M.B.B.S", Value = "M.B.B.S"},
new SelectListItem { Text = "B.A", Value = "B.A"}
}
)
You Can Use this code But If You Have Data Base For This DropDownList
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900