DropDownListFor in EditorTemplate not selecting value

Viewed 42183

I have an editor template for a custom object. Inside that editor template I use a couple of DropDownListFor helpers. In each of them I specify a unique model property (with the pre-selected value) and the select list containing all the select options.

Example:

<%=Html.DropDownListFor(m => m.DocumentCategoryType, Model.DocumentCategoryTypeList) %>

I know that the option values are being populated (from viewing source) and that my Model is passed in with the correct ID value (DocumentCategoryType).

When the view is rendered, there is no selected item in my dropdown and therefore it defaults to the first (non-selected) value.

Does anyone have any ideas?

Thanks.

12 Answers

We also solved the solution by populating a new SelectList that has the appropriate SelectListItem selected, but created this extension method to keep the call to DropDownListFor a little cleaner:

public static SelectList MakeSelection(this SelectList list, object selection)
{
    return new SelectList(list.Items, list.DataValueField, list.DataTextField, selection);
}

Then your DropDownListFor call becomes:

<%= Html.DropDownListFor(m => m.DocumentCategoryType, Model.DocumentCategoryTypeList.MakeSelection(Model.DocumentCategoryType)) %>

It is confirmed as a bug @ aspnet.codeplex.com and only behaves like this for strongly typed views.

Workaround: populate your SelectList in the view code

like

<%= Html.DropDown("DocumentCategoryType", new SelectList(Model.Categories,"id","Name",Model.SelectedCategory")) =>

Yuck. I ended up solving it like this. I hope this gets fixed for RTM.

   <%if(Model!=null){ %>
        <%= Html.DropDownListFor(m => m.DocumentCategoryType, new SelectList(Model.DocumentCategoryTypeList,"Value","Text", Model.DocumentCategoryType))%>
        <%}else{%>
            <%=Html.DropDownListFor(m => m.DocumentCategoryType, Model.DocumentCategoryTypeList) %>
        <%}%>
Model.DocumentCategoryTypeList

This is probably your problem. On the SelectListItems, do you set the value to the .ToString() output?

 var list = new List<SelectListItem>()
                           {
                               new SelectListItem()
                                   {
                                       Value = Category.Book.ToString(),
                                       Text = "Book"                                     
                                   },
                               new SelectListItem()
                                   {
                                       Value = Category.BitsAndPieces.ToString(),
                                       Text = "Bits And Pieces"                            },
                               new SelectListItem()
                                   {
                                       Value = Category.Envelope.ToString(),
                                       Text = "Envelopes"                              }
                           };

Works for me after doing that. It just needs to be able to match the value from the object

Related