Setting selected item to DropdownList in MVC Appli

2019-06-04 21:59发布

I am having an MVC application in that I am having an dropdownList on the add client page. The user selects 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

1条回答
太酷不给撩
2楼-- · 2019-06-04 22:19

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"]) %>
查看更多
登录 后发表回答