如鹏的学习管理系统是使用ASP.net MVC 5开发的,今天一个新版本发布后网站出现一个Bug,学生在下拉列表中选中的项再加载显示的时候发现仍然没被选中。详细一点说吧:
假如有这样一个Action:
| 1 2 3 4 5 6 7 8 | publicActionResult Index(){List<SelectListItem> persons = newList<SelectListItem>(); persons.Add(newSelectListItem { Text = "腾讯", Value = "qq"});persons.Add(newSelectListItem { Text = "如鹏", Value = "rupeng", Selected = true});ViewBag.persons = persons;returnView();} | 
Cshtml是这样的:
@Html.DropDownList("persons", (IEnumerable<SelectListItem>)ViewBag.persons)
生成的html是这样的:
| 1 2 3 4 | <selectid="persons" name="persons"><optionvalue="qq">腾讯</option><optionvalue="rupeng">如鹏</option></select> | 
竟然第二项没有处于选中状态,太诡异了吧!
  只要把DropDownList第二个参数的"persons"改成和”ViewBag.persons”的persons名字不一样就可以,比如:
@Html.DropDownList("persons1", (IEnumerable<SelectListItem>)ViewBag.persons)
   这样就正确生成了:
| 1 2 3 4 | <selectid="persons1" name="persons1"><optionvalue="qq">腾讯</option><optionselected="selected" value="rupeng">如鹏</option></select> | 
好诡异!!!
   咋办?看源码!
   DropDownList是定义在SelectExtensions扩展类中,DropDownList方法最终是调用SelectInternal方法,核心代码是这一段:
| 1 2 3 4 5 6 7 8 | if(!flag && obj == null&& !string.IsNullOrEmpty(name)){obj = htmlHelper.ViewData.Eval(name);}if(obj != null){selectList = SelectExtensions.GetSelectListWithDefaultValue(selectList, obj, allowMultiple);} | 
这个name参数就是我们传递给DropDownList的第一个参数"persons",上面代码主要逻辑就是:首先到ViewData中查找名字为"persons"的值,我们知道ViewData和ViewBag是一样的,所以htmlHelper.ViewData.Eval(name)取到的值就是我们在Index中定义的List<SelectListItem>()集合。GetSelectListWithDefaultValue方法的代码是:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | privatestaticIEnumerable<SelectListItem> GetSelectListWithDefaultValue(IEnumerable<SelectListItem> selectList, objectdefaultValue, boolallowMultiple){IEnumerable enumerable= newobject[]{defaultValue};IEnumerable<string> collection = fromobjectvalue inenumerableselectConvert.ToString(value, CultureInfo.CurrentCulture);HashSet<string> hashSet = newHashSet<string>(collection, StringComparer.OrdinalIgnoreCase);List<SelectListItem> list = newList<SelectListItem>();foreach(SelectListItem current inselectList){current.Selected = ((current.Value != null) ? hashSet.Contains(current.Value) : hashSet.Contains(current.Text));list.Add(current);}returnlist;} | 
注意,我们的List<SelectListItem>()集合被当成defaultValue参数传递给GetSelectListWithDefaultValue方法了(why?),在方法内部又把defaultValue给
 
Convert.ToString()一下,变成了”System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]”这么一个玩意,
 
GetSelectListWithDefaultValue的主要逻辑就是查找selectList中等于”System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]”的值,能找到才算见了鬼呢!!!
经过上面的分析我们还可以知道,不能让cshtml中DropDownList的第一个name参数和ViewBag中任何一个属性重名,否则还是会有问题,比如
| 1 2 3 4 5 6 7 8 9 | publicActionResult Index(){List<SelectListItem> persons = newList<SelectListItem>(); persons.Add(newSelectListItem { Text = "腾讯", Value = "qq"});persons.Add(newSelectListItem { Text = "如鹏", Value = "rupeng", Selected = true});ViewBag.persons = persons;ViewBag.persons1 = newstring[] { };returnView();} | 
Cshtml如下:
 @Html.DropDownList("persons1", (IEnumerable<SelectListItem>)ViewBag.persons)
生成的html中第二条数据照样不会被selected
不知道微软为什么把DropDownList这么简单的一个东西搞的这么复杂,正验证了这句话“写的越多,错的越多”。当然也许微软会给出理由说我们用错了,说“It’s not a bug,It’s a feature,by design”好吧!谢特!
如鹏网.Net培训班正在报名,有网络的地方就可以参加如鹏网的学习,学完就能高薪就业,点击此处了解
三年前只要懂“三层架构”就可以说“精通分层架构”;现在则需要懂IOC(AutoFac等)、CodeFirst、lambda、DTO等才值钱;
三年前只要会SQLServer就可以说自己“精通数据库开发”;现在则需还需要掌握MySQL等开源数据库才能说是“.Net开源”时代的程序员;
三年前只要会进行用户上传内容的安全性处理即可;现在则需要熟悉云存储、CDN等才能在云计算时代游刃有余;
三年前只要掌握Lucene.Net就会说自己“熟悉站内搜索引擎开发”;现在大家都用ElasticSearch了,你还用Lucene.Net就太老土了;
三年前发邮件还是用SmtpClient;现在做大型网站发邮件必须用云邮件引擎;
三年前缓存就是Context.Cache;现在则是Redis、Memcached的天下;
如鹏网再次引领.Net社区技术潮流!点击此处了解如鹏网.Net最新课程
| 本博客Android APP 下载 | 
|  | 
| 支持我们就给我们点打赏 | 
|  | 
| 支付宝打赏 支付宝扫一扫二维码 | 
|  | 
| 微信打赏 微信扫一扫二维码 | 
|  | 
如果想下次快速找到我,记得点下面的关注哦!
 
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号