Setting selected item to DropdownList in MVC Application?
I am having an MVC application in that I am having an dropdownList on the add client page. The user sele开发者_运维百科cts the sates in the dropdownlists.
On the edit Page,I want the same state selected which the user selected on the add client page please tell what shall I do.
Thank You Ritz
Try to set selected option at data source (SelectList) like that :
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("State 1", "1");
list.Add("State 2", "2");
list.Add("State 3", "3");
var selectList = new SelectList(list,
"Value", "Key",
"2"); // selected item's value "State 2" is selected.
ViewData["States"] = selectList;
<%= Html.DropDownList("ddlStates", (SelectList)ViewData["States"]) %>
精彩评论