HTML dropdownlists in MVC3
I can get this to work.
<div class="editor-field">@Html.DropDownListFor(model => model.TopicDescription,
new SelectList(ViewBag.TopicDescription, "TopicDescription", "TopicDescription"))
</div>
BUT my code doesn't look clean as in the Topic class I have a field called TopicDescription. I wanted to tidy it up so I changed that class field to be called Description.
<div class="editor-field">@Html.DropDownListFor(model => model.TopicDescription,
new SelectList(ViewBag.Description, "Description", "Description"))
</div>
However now my selection drop down list is not correctly populated on load. My questions is concerning this. Does anyone know if开发者_开发技巧 there is a connection between the names in the drop down list and the names in the selection. Somehow is MVC linking the two.
If you see the SelectList
constructor definition here , the first one is the object (IEnumerable
), and the second and third are dataValueField
and dataTextField
respectively. so these values must be matched to the name of properties of object contained in the first parameter
精彩评论