开发者

Persist SelectedIndex of DropDownList in ASP.NET MVC

开发者 https://www.devze.com 2023-02-04 11:24 出处:网络
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

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!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号