Persist SelectedIndex of DropDownList in ASP.NET MVC
I want to automatically select the last index which was selected before called the action. Inside the action I put the selected index into the ViewData["SelectedI开发者_如何学JAVAndex"]. But now how do I set the DropDownList with the value set in the ViewData.
john,
try something like this (based on your own strongly typed view model):
<%=Html.DropDownListFor(m => m.EquipmentID, new SelectList(Model.Equipment, "id", "Name", ViewData["SelectedIndex"]), "-- Select Equipment --") %>
might (should!!) just work.
[edit] - you may have to pass in the 'name' property to ViewData (i.e. ViewData["SelectedIndex"] = item.Name; ), rather than the id in the above. just thought more about it there!!
精彩评论