开发者

Setting selected item to DropdownList in MVC Application?

开发者 https://www.devze.com 2022-12-16 20:48 出处:网络
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.

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

精彩评论

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

关注公众号