Link to home
Start Free TrialLog in
Avatar of hijiki7777
hijiki7777

asked on

ASP.Net MVC 2 setting a DropDownListFor item

I am new to MVC and I am starting at version 2. I am populating a View with a row of data that contains an Id field (Status). This Id field is bound to a list of Status strings displayed in a drop down list. In my controller I populate a SelectList collection in which the current Status is correctly selected (I checked this). However the View always selects the first item on the list regardless. So what is wrong with this?
<div class="editor-field">
            <%: Html.DropDownListFor(model => model.Status, ViewData["Status"] as IEnumerable<SelectListItem>)%>
            <%: Html.ValidationMessageFor(model => model.Status) %>
        </div>

Open in new window

Avatar of nmarun
nmarun
Flag of India image

Please see the link below to see how exactly to set the selected item on a dropdownlist:

http://stackoverflow.com/questions/624828/asp-net-mvc-html-dropdownlist-selectedvalue

Arun
ASKER CERTIFIED SOLUTION
Avatar of jamesbaile
jamesbaile
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hijiki7777
hijiki7777

ASKER

Spot on.
I think I should use the View Model from now on.
If you look at most ASP.NET MVC best practice recommendations they advise using the Model rather than using magic strings which are error prone.