ASP.NET MVC 3 DropDownListFor strange behaviour for determining selected option

So a strange behaviour in the implementation of the HtmlHelper.DropDownListFor extension method was recently brought to my attention where the order of 'looking for' the selected value of the list seemed incorrect.

I would assume that the selected value would always be the selected value from the strongly typed model property (as I have clearly stated by providing a lambda which results in returning that property) but it seems the selected value will not always be the selected value from the strongly typed model as I would expect.

For example:

@model TestModel

@{
    Model.Title = "Hello";
    ViewBag.Title = "Tim";
    Layout = null;
}

@using (Html.BeginForm())
{
    @Html.DropDownListFor(p => p.Title, new SelectList(new[] { "Other", "Hello", "Brent","Tim","Smith" }, "Smith"))
}

Given the above code what would the value of the drop down list be?
a) "Smith" b) "Tim" c) "Hello"

Well I would expect the SelectList constructor to create a list with a selected value of "Smith" then I would expect the DropDownListFor extension method to set the selected value to be "Hello" as provided by my model.

In fact what happens is that the value from the ViewBag is taken in priority over any others!
The answer to above is actually b).

To be honest using a strongly typed view I don't think I would ever want the extension method to take the value from the ViewBag implicitly.

Now if I remove the Title parameter in the ViewBag as done so below, what would you expect?

@model TestModel

@{
    Model.Title = "Hello";
    //ViewBag.Title = "Tim";
    Layout = null;
}

@using (Html.BeginForm())
{
    @Html.DropDownListFor(p => p.Title, new SelectList(new[] { "Other", "Hello", "Brent","Tim","Smith" }, "Smith"))
}

Using the above logic the value would be "Hello" which is correct. However this doesn't give us an option to override the selected value as the SelectList's selected overload does the work before handing the object off to the DropDownListFor extension method which it then ignores.

Finally if the ViewBag property is null AND my Model's property is null then the SelectList's selected value will be unchanged and the result will be "Smith".

Now I looked into TextBoxFor and EditorFor, etc and they work as expected. It is only the ListBoxFor and DropDownListFor extension methods which are first checking for the value in the ViewBag before the Model.

Just something everyone needs to be aware of. I would consider this a bug. Be sure to check your naming as we had a page title in the viewbag called "ViewBag.Title" and a "Person.Title" property on our strongly typed Person model. The select list tried to set the selected value of the list to be the page's title!.

comments powered by Disqus