Home > DeveloperSection > Forums > Dropdownlist selected value in asp.net mvc
Manoj Bhatt
Manoj Bhatt

Total Post:154

Posted on    October-08-2014 12:16 AM


 1 Reply(s)
 1058  View(s)
Rate this:
 have tried this is RC1 and then upgraded to RC2 which did not resolve the issue.

// in my controller

        ViewData["UserId"] = new SelectList(




        selectedUserId.Value); // this has a value

result: the SelectedValue property is set on the object.

// in my view

        <%=Html.DropDownList("UserId", (SelectList)ViewData["UserId"])%>

result: all expected options are rendered to the client, but the selected attribute is not set. The item in SelectedValue exists within the list, but the first item in the list is always defaulted to selected.

Kamlakar Singh
Kamlakar Singh

Total Post:194

Posted on    October-08-2014 12:18 AM

Try this:

public class Person


            public int Id { get; set; }

            public string Name { get; set; }


And then:

var list = new[] {  

            new Person { Id = 1, Name = "Name1" },

            new Person { Id = 2, Name = "Name2" },

            new Person { Id = 3, Name = "Name3" }



        var selectList = new SelectList(list, "Id", "Name", 2);

        ViewData["People"] = selectList;


        Html.DropDownList("PeopleClass", (SelectList)ViewData["People"])

With MVC RC2, I get.

<select id="PeopleClass" name="PeopleClass">

    <option value="1">Name1</option>

    <option selected="selected" value="2">Name2</option>

    <option value="3">Name3</option>


Modified On Oct-08-2014 12:20:38 AM

Don't want to miss updates? Please click the below button!

Follow MindStick