C# mvc DropDownList选中状态无效情况分析
情况:
DropDownList控件使用List<SelectListItem>()设置下拉选项和默认值。
当控件的Name和后台的ViewBag(或ViewData)的Key重复,会导致选中状态无效。
规则如下
1、DropDownList数据源可从ViewBag(或ViewData)中同名Key中自动获取
例如,下面这种写法可自动为控件加下拉选项:
Controller中:ViewData["MyList"] = new List<SelectListItem>(){...};
View中:@Html.DropDownList("MyList")//DropDownList无直接数据源
2、DropDownList数据源优先从DropDownList直接的数据源获取。若DropDownList有直接数据源,则ViewData同名Key值(应该是ViewData[Key] as string)只作为默认选中值(见3)
如下情况,都使用SelectListItem,类似于1,option为view的,选中为空
Controller中:ViewData["MyList"] = new List<SelectListItem>(){...};
View中:@Html.DropDownList("MyList", new List<SelectListItem>(){...})//DropDownList有直接数据源
3、DropDownList选中值优先从ViewBag(或ViewData)中同名Key中自动获取
例如,下面这种写法,优先选中的是viewdata中value为"optionvalue"的选项:
Controller中:ViewData["MyList"] = "optionvalue";
View中:@Html.DropDownList("MyList", new List<SelectListItem>(){...})
所以,如上情况,对应的原因同规则2所述,DropDownList优先用了ViewData中的数据作为默认选中值,由于ViewData中的数据dataDic是Dictionary<string,string>类型,DropDownList会认为选中值为(dataDic as string)....后果就是谁都没选中
以上几个规则都是测试出来的结果,未翻看mvc源码,若有不对多有包涵,欢迎指正。
C# mvc DropDownList选中状态无效情况分析的更多相关文章
- JQuery Mobile - 修改复选框的选中状态无效解决办法!
今晚,在编写JQuery Mobile程序时候,需要在代码里面控制复选框的选中状态,很简单的代码啊,很快完成了!等测试程序时候傻眼了,页面无论如何也不按照我写的代码显示出来!问题出在哪里呢?是我写的控 ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- LayUI左侧菜单无法保持选中状态
1.问题描述:一般的左侧菜单都会是动态添加的模块,利用循环把模块名和链接地址逐个显示出来如下图 但是问题来了,只要点任何二级菜单就不会保持左侧菜单当前一级菜单和二级菜单选中状态. 2.分析原因:因为模 ...
- MVC dropdownlist 后端设置select属性后前端依然不能默认选中的解决方法
-----------------------------------来自网上的解决方法--------------------------------------------- ASP.Net MV ...
- @Html.DropDownList 设置选中值无效
有时候在ASP.NET MVC中用@Html.DropDownList 设置选中值无效,如图: 具体原因说不清,反正只要改个名字就行了!!!,如图:::
- jquery设置checkbox状态,设置dropdownlist选中值,隐藏某控件,给某控件追加东西
jquery设置checkbox状态 $("[ID$=chkType]").attr("checked", true); jquery设置dropdownlis ...
- RecyclerViewSelectableAdapterDemo【封装BaseSelectableAdapter用于多选、单选,以及切换选中状态等功能】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录封装单选.多选.切换选中状态的BaseSelectableAdapter基类,配合Recyclerview使用. 注意:此Dem ...
- 如何在 messager/alert/confirm等消息提示框中 获取 / 设置 嵌入 html内容中的 input[type=checkbox]等的选中状态?
总结, 有3点: 不能/不要 在 这些消息框 / 提示框/ 对话框中的 回调函数中去写代码: 获取嵌入 内容中input.checkbox的选中状态, 因为 虽然在这些框存在的时候, 这个 check ...
- 怎么取得dropdownlist选中的ID值
把数据库绑定在dropdownlist中,然后把选中的dropdownlist的项的ID值保存在另外的一个数据库中.怎么取得dropdownlist选中的ID值呢?? this.DropDownLis ...
随机推荐
- Reactor模式通俗解释
Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...
- Delphi 调用Dll的两种方式
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- arcgis_engine_develop_error_42
解决: 今天在VS2013打开程序时,手工添加了pageLayoutControl隔一会弹出错误窗口:Provide your license server administrator with th ...
- Iphone连接win10电脑正常, itunes无法识别解决
1.之前试过重装itunes, apple的两个服务也都已经启动. 2.但是设备管理器不显示移动设备或者usb的apple mobile device driver 3.解决:打开C:\Program ...
- leetcode 206
206. Reverse Linked List Reverse a singly linked list. 翻转一个单链表. 代码如下: /** * Definition for singly-li ...
- Orcle学习(一)
exists"和"in"的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ...
- Convert HTML to Text(转载)
原文地址:http://www.blackbeltcoder.com/Articles/strings/convert-html-to-text Download Source Code Intro ...
- Modified Least Square Method and Ransan Method to Fit Circle from Data
In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...
- Generate SQL from Excel
Tips: SUBSTITUTE(D4,"'","''")---if D4 contain ', this function will convert ' ...
- ConCurrent in Practice小记 (1)
ConCurrent in Practice小记 (1) 杂记,随书自己写的笔记: 综述问题 1.线程允许在同一个进程中的资源,包括共享内存,内存句柄,文件句柄.但是每个进程有自己的程序计数器,栈和局 ...