将所选项目设置为MVC应用程序中的DropdownList? [英] Setting selected item to DropdownList in MVC Application?

查看:67
本文介绍了将所选项目设置为MVC应用程序中的DropdownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序,因为我在添加客户端页面上有一个dropdownList. 用户在下拉列表中选择状态.

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.

谢谢 丽兹

推荐答案

尝试在数据源(SelectList)上设置选定的选项,如下所示:

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

这篇关于将所选项目设置为MVC应用程序中的DropdownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆