mercredi 4 février 2015

MVC5 Html.DropDownListFor Does not select correct value

I'm doing something horribly simple and it isn't working.



@Html.DropDownListFor(m => m.ContentDefinitionID, Model.ContentBoxesSelectList, new {@class = "form-control"})


The ContentDefinitionID is a UInt64 (although I've tried an int)


I use this same select list for 4 different controls on the page.


If my Model.ContentDefinition is set to 4, (which would be "test4" in the drop down) then it SHOULD pull that selected value from the Model, NOT from the selectList right? Someone else said that it ignores the value on the SelectList when you use the m=>m.X syntax - which makes sense.


But it always selects the first one. I tried adding another parameter for what it should select, but it wants the text, not the value and I hate to have to lookup the text. (And I'm not sure this will post back correctly if I do that anyway)


I'm about to go create some JQuery code to set the default values for everything -but that is crazy. This is the obvious behavior of the DropDownListFor() method, kind of a 'duh' thing - why doesn't it work? Do I need to create a custom SelectList for every control that exists on the page?





Aucun commentaire:

Enregistrer un commentaire