Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Closed Issue: DropDownListFor selected value not being set when model property has indexer [1517]

$
0
0
Vote number - 11

In MVC 3 when using DropDownListFor within a loop, i.e. the model property has an indexer, the DropDownListFor helper doesn't set the selected option.

E.g.

@for (var i =0; i < Model.Products.Count; i++) {
@Html.DropDownListFor(m => m.Products[i].CategoryId, new SelectList(Model.Categories, "Id", "Name"))
}

All items are displayed with the first option in the list.

Using the TextBoxFor helper works perfectly within the loop:

E.g.
@for (var i = 0; i < Model.Products.Count; i++) {
@Html.TextBoxFor(m => m.Products[i].CategoryId)
}

displays the correct category Id.

After having a bit of a look at the MVC 3 source it looks like TextBoxFor and DropDownListFor look up the model value using different mechanisms. TextBoxFor uses ModelMetadata.FromLambaExpression() which finds the value correctly. DropDownListFor uses htmlHelper.ViewData.Eval(fullName) which returns null if there is an indexer in the model expression.


__comments__

apboyle wrote Apr 15, 2012 at 3:29 PM
@ChuckP You can include the value in the SelectList constructor, e.g. modied from above: @Html.DropDownListFor(m => m.Products[i].CategoryId, new SelectList(Model.Categories, "Id", "Name", Model.Products[i].CategoryId))

ChuckP wrote Apr 16, 2012 at 6:29 AM
@Html.DropDownListFor(m => m.Products[i].CategoryId, IEnumerable) is also broke.
Comments: Verified

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>