0

I'm using ASP.NET MVC 2.0, but apparently this was a problem in MVC 3.0 as well.

Basically if you have a "complex" property name, then Html.DropDownListFor does not set any SelectListItem.Selected = true.

So this works (where ViewData.States is IEnumerable<SelectListItem>).

<td><%= Html.DropDownListFor( m => m.State, ViewData.States ) %></td>

But this does not:

<td><%= Html.DropDownListFor( m => m.CaseFields[i].State, ViewData.States ) %></td>

I saw this QA where the answerer correctly explained the problem, however his solution of using SelectList as the argument to DropDownListFor does not work in my case - I get the exact same result (loads of <option> elements but not a single selected="selected" attribute).

I'm tempted to reimplement DropDownListFor, but if anyone knows a workaround I'd love to hear it!

Community
  • 1
  • 1
Dai
  • 141,631
  • 28
  • 261
  • 374

1 Answers1

0

I solved this by reimplementing DropDownListFor by copying the source from the MVC project on CodePlex and making a few changes.

The main change I made was to replace the call to ViewData.Eval (which in MVC 2 and 3 does not support indexed properties) with ModelMetadata.FromLambdaExpression().Model, and it works great.

I also added support for numeric Enum values too, as DropDownListFor only binds enum values if you use the string (name) value rather than the numeric value.

Dai
  • 141,631
  • 28
  • 261
  • 374