0

I have a MVC 5 application using a lot of the standard features. I have a number of dropdownlists across the app that render properly, except for one.

In the Controller:

ViewBag.TitleLookup = new SelectList(db.TitleLookups, "Title", "Title", person.Title);
return View(person);

In the View:

@Html.DropDownList("Title",(SelectList)ViewBag.TitleLookup,string.Empty)
@Html.DropDownList("TitleLookup", string.Empty)

Both dropdown lists render the options properly, however, the first does not have the properly selected item. The second has the wrong Name so would not be submitted to the server on a post, however it did have the "valid" property indicating the validation was active.

I have stepped through the controller and the SelectListItems are getting built correctly along with the selected property on the proper Item getting set to True.

So to ask a concrete question: Why does the first dropdownlist not render the proper selected item? Is there indeed something crazy about having the property called "Title"?

I can change the datamodel if I absolutely have to, but feels like it should be able to work without doing that.

  • I keep playing with this. If I change the Name of the first dropdownlist to "Duck", it too renders fine, but would not have the proper Name to be transmitted to the server. It really does seem like the word "Title" is here the culprit. – GroundedTraveler May 04 '14 at 14:06
  • Ahhh ... that would make sense seeing how I mentioned in my answer that controls in MVC will have issues binding when they overlap with viewbag/viewdata properties. And ViewBag.Title is used by MVC. – Erik Elkins May 04 '14 at 14:53

1 Answers1

1

It looks like you have put in "Title" for the second and third parameters on the select list. Those parameters are for complex objects to select those properties, say Key and Value if you're binding to a dictionary (I'm assuming db.TitleLookups is a list of strings). Instead, try this:

new SelectList(db.TitleLookups, person.Title);

In addition, MVC might have issues with controls named after a ViewData/ViewBag property. See: ASP.NET MVC Html.DropDownList SelectedValue

Community
  • 1
  • 1
Erik Elkins
  • 629
  • 5
  • 14
  • Yeah, the lookup table is dead simple with only the one field. I get that those arguments are for telling it how to build the select list. As I mentioned in the question, I have looked through the created SelectList and it looks fine. It also works for the second DropDownList in the View. I really don't think it is a problem with the Controller code building the list. – GroundedTraveler May 04 '14 at 14:04
  • The real answer is in the link, not in the line of code. As you say, the ViewBag cannot contain duplicate field names with what needs to be rendered as fields in the form. – GroundedTraveler May 06 '14 at 12:46
  • You have got to be kidding me! I spent way too long on this, I had a 'Title' column in my db which would not select in a DropDownListFor. After trying every possible variation of syntax I found this post. After updating to StaffTitle it works as expected. – DanIreland Jun 19 '19 at 10:03