HeartattacK

Asp.net MVC, Html.DropDownList and Selected Value

I recently ran into the altogether common problem of the Html.DropDownList helper rendering a drop down list with no value selected. This is a major problem when editing data as by default, the first value is selected and saving would mean the first value is used.

There have been a few issues resulting in the same error. My issue was that I was setting the Name of the drop down list to be equal to the property on my model. I was using the Entity Framework, and had an Image class with a navigation property called Category. I was using this to render the ddl:

<%= Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewData["categories"])%>

In my controller, I was setting the ViewData like this:

this.ViewData["categories"] = new SelectList(db.CategorySet.ToList(), "CategoryId", "Title", img.CategoryReference.EntityKey);

Unfortunately, even though I had set the selected value (third parameter to the SelectList constructor), the ddl had no value selected.

The fix was quite simple:

<%= Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewData["categories"])%>

I just changed the Name of the drop down and handled the assignment in the controller.

The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.

 

Hope that helps.

Comments

ASP.NET MVC Archived Blog Posts, Page 1 said:

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# March 28, 2009 12:36 AM

Web Development Community said:

You are voted (great) - Trackback from Web Development Community

# March 30, 2009 1:58 AM

Martin Evans said:

Thanks very much. This seems to be the only example any where on the internet where the viewdata is cast to IEnumerable<SelectListItem> in the markup!

# March 31, 2009 8:14 AM

Jeremy said:

Can you show the related code? i.e., CategorySet, CategoryReference, the controller method code?

# April 2, 2009 11:28 AM

HeartattacK said:

Sorry for the delay in getting back to this..I was setting up my new apartment, and it took a LOT of work.

The related code is very simple.

img is an Image (not System.Drawing.Image, just an Entity).

Each image has a Category. Each category can have one or mor Images.

I'm using The Entity Framework, and CategorySet is the plural of Category.

-------------------

The controller to handle the postback is like this:

public ActionResult Edit(string ImageId, string Description, int Rating, string CategoryId)

       {

           Guid imageId = new Guid(ImageId);

           var img = db.ImageSet.Where(x => x.ImageId == imageId).First();

           try

           {

               img.Description = Description;

               img.Rating = Rating;

               img.CategoryReference.EntityKey = new System.Data.EntityKey("StudiomorphEntities.CategorySet",

                  "CategoryId", new Guid(CategoryId));

.......

db.SaveChanges();

# April 27, 2009 9:03 AM

Matt said:

I was experiencing a similar problem but I was eventually able to get the selected value to show without matching the name of the dropdown to a property of the select list.

HTML -

<% =Html.DropDownList("PropertyStyleId", (SelectList)ViewData["PropertyStyles"], "(Select Style)")%>

Controller Code -

ViewData["PropertyStyles"] = new SelectList(_typeRepository.GetPropertyStyles(), "TypeId", "TypeDescription", t);

# April 28, 2009 2:43 PM

www.almny.com said:

can i get the source code for how to bind dropdownlist from mvc

# May 6, 2009 8:57 AM

Shailja Agrawal said:

This also works but the main reason of dropdownlist not showing the selected value is that the page is beingposted back whenever a button is clicked and thus the page_load event handler is called which in turn resets the selected value property , so to rectify just check in the page_load handler that wthr it is being posted back or being loaded for the first time , by inserting if page.ispostback = false.

# May 14, 2009 5:34 AM

HeartattacK said:

Shailja, this is about MVC, not webforms.

# May 14, 2009 12:59 PM

www.almny.com said:

thanks for your reply

# May 30, 2009 3:13 PM

Aaron Clausen said:

Thanks.

I discovered the same bug. Your fix worked.

Cheers

# June 15, 2009 9:56 PM

Brasileiro said:

Puta que pariu! Quero fórum em Português!! Não entendo nada que vcs falam!!!

# June 18, 2009 9:33 AM

kys said:

This issue was driving me nuts. I was tracing the SelectListItem enumeration and found that the item that I marked as selected was indeed kept selected but it was just not reflecting in the DropDownList. So, as you said, I changed the name of the DropDownList to match the property name on the model, and it worked fine. Thanks for the post. Much appreciated.

# July 20, 2009 2:17 AM

pdb said:

This really helped! Thanks!

# July 25, 2009 12:36 PM

Joel said:

Oi Brasileiro, se queresse em portugues, porque nao utilize o google translate?

# October 8, 2009 11:24 AM

Debugging, MVC TextAreas and Drop Downs Lists « Yet Another Middle Manager said:

Pingback from  Debugging, MVC TextAreas and Drop Downs Lists &laquo; Yet Another Middle Manager

# December 5, 2009 11:00 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)