Dropdownlist value not being set when there is a default blank option

Posted by uriDium on Stack Overflow See other posts from Stack Overflow or by uriDium
Published on 2010-05-25T19:54:24Z Indexed on 2010/05/25 20:21 UTC
Read the original article Hit count: 170

Filed under:

I am using ASP.Net MVC.

I have a partial view which has a form with dropdownlists. The dropdownlists are set via ViewData. The partial view is used in a Create and Edit page. The create works fine. I get the dropdownlists and the blank option is a "Please select", like so

<%= Html.DropDownList("ContactNrType", ViewData["ContactNrType"] as SelectList, "Please Select") %>

But this doesn't seem to work for my edit. If I have that extra "Please select" parameter then it does not select the value for the drop down. I am setting the value of the drop down in the controller like so

ViewData["ContactNrType"] = new SelectList(new List<string> { "Mobile", "Home", "Work", "Friend" }, candidate.ContactNrType);

Any idea as to what I am doing wrong?

I want to share the partial view which contains the form between the two pages. So I need the "Please Select" option for the Create. And I need the value set for the Edit (I don't mind that it has an option that still says "Please Select").

© Stack Overflow or respective owner

Related posts about asp.net-mvc