Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownListFor in EditorTemplate not selecting value

Tags:

asp.net-mvc

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.

like image 240
kmehta Avatar asked Dec 16 '09 17:12

kmehta


2 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)) %> 
like image 61
Curtis Buys Avatar answered Oct 06 '22 00:10

Curtis Buys


Looking through the ASP.NET MVC 2 source code reveals some solutions to this problem. Essentially, any SelectListItem in the SelectList passed in the helper extension method that has the Selected property set to true does not have any bearing over the <option> element rendered with the selected attribute applied for the item.

The selected attribute on <option> elements is determined by

1) checking that the helper extension method was passed a SelectList. If this is null, the framework will look in the ViewData for a value corresponding to the key that is the view model property for which you wish to render the drop down list for. If the value is a SelectList, this will be used to render the <select> including taking any selected values, so long as the model state for the model property is null.

2) If a SelectList has been passed in the helper extension method and the model state for the model property is null, the framework will look in the ViewData for a default value, using the model property name as the key. The value in view data is converted to a string and any items in the SelectList passed to the helper extension method that have a value (if no value is set, then the Text will be checked) that matches the default value will have the Selected property set to true which in turn will render an <option> with the attribute selected="selected".

Putting this together, there are two plausible options that I can see to have an option selected and use the strongly typed DropDownListFor:

Using the following view model

public class CategoriesViewModel {     public string SelectedCategory { get; private set ; }     public ICollection<string> Categories { get; private set; }      public CategoriesViewModel(string selectedCategory, ICollection<string> categories)     {         SelectedCategory = selectedCategory;         Categories = categories;     } } 

Option 1

Set a value in the ViewData in the controller rendering your view keyed against the property name of the collection used to render the dropdown

the controller action

public class CategoriesController {     [HttpGet]     public ViewResult Select()     {         /* some code that gets data from a datasource to populate the view model  */         ICollection<string> categories = repository.getCategoriesForUser();         string selectedCategory = repository.getUsersSelectedCategory();          CategoriesViewModel model = new CategoriesViewModel(selectedCategory, categories);         this.ViewData["Categories"] = selectedCategory;                  return View(model);     }      [HttpPost]     public ActionResult Select(CategoriesViewModel model)     {         /* some code that does something */     } } 

and in the strongly typed view

<%: Html.DropDownListFor(m => m.Categories, Model.Categories.Select(c => new SelectListItem { Text = c, Value = c }), new { @class = "my-css-class" }) %> 

Option 2

Render the dropdown using the name of the property of the selected item(s)

the controller action

public class CategoriesController {     [HttpGet]     public ViewResult Select()     {         /* some code that gets data from a datasource to populate the view model  */         ICollection<string> categories = repository.getCategoriesForUser();         string selectedCategory = repository.getUsersSelectedCategory();          CategoriesViewModel model = new CategoriesViewModel(selectedCategory, categories);          return View(model);     }      [HttpPost]     public ActionResult Select(CategoriesViewModel model)     {         /* some code that does something */     } } 

and in the strongly typed view

<%: Html.DropDownListFor(m => m.SelectedCategory, Model.Categories.Select(c => new SelectListItem { Text = c, Value = c }), new { @class = "my-css-class" }) %> 
like image 32
Russ Cam Avatar answered Oct 06 '22 01:10

Russ Cam