Setting selected item to DropdownList in MVC Appli

2019-06-04 22:16发布

问题:

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:

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"]) %>