星空网 > 软件开发 > ASP.net

ASP.Net MVC的ViewBag一个坑,不要跳进去

   如鹏的学习管理系统是使用ASP.net MVC 5开发的,今天一个新版本发布后网站出现一个Bug,学生在下拉列表中选中的项再加载显示的时候发现仍然没被选中。详细一点说吧:
假如有这样一个Action:

public ActionResult Index(){List<SelectListItem> persons = new List<SelectListItem>(); persons.Add(new SelectListItem { Text = "腾讯", Value = "qq" });persons.Add(new SelectListItem { Text = "如鹏", Value = "rupeng", Selected = true });ViewBag.persons = persons;return View();}

  Cshtml是这样的:

@Html.DropDownList("persons", (IEnumerable<SelectListItem>)ViewBag.persons)
生成的html是这样的:

<select id="persons" name="persons"><option value="qq">腾讯</option><option value="rupeng">如鹏</option></select>

   竟然第二项没有处于选中状态,太诡异了吧!

  只要把DropDownList第二个参数的"persons"改成和”ViewBag.persons”的persons名字不一样就可以,比如:
@Html.DropDownList("persons1", (IEnumerable<SelectListItem>)ViewBag.persons)
   这样就正确生成了:

<select id="persons1" name="persons1"><option value="qq">腾讯</option><option selected="selected" value="rupeng">如鹏</option></select>

  

   好诡异!!!


   咋办?看源码!
   DropDownList是定义在SelectExtensions扩展类中,DropDownList方法最终是调用SelectInternal方法,核心代码是这一段:

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方法的代码是:

private static IEnumerable<SelectListItem> GetSelectListWithDefaultValue(IEnumerable<SelectListItem> selectList, object defaultValue, bool allowMultiple){IEnumerable enumerable= new object[]{defaultValue};IEnumerable<string> collection = from object value in enumerableselect Convert.ToString(value, CultureInfo.CurrentCulture);HashSet<string> hashSet = new HashSet<string>(collection, StringComparer.OrdinalIgnoreCase);List<SelectListItem> list = new List<SelectListItem>();foreach (SelectListItem current in selectList){current.Selected = ((current.Value != null) ? hashSet.Contains(current.Value) : hashSet.Contains(current.Text));list.Add(current);}return list;}

  

注意,我们的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中任何一个属性重名,否则还是会有问题,比如

public ActionResult Index(){List<SelectListItem> persons = new List<SelectListItem>(); persons.Add(new SelectListItem { Text = "腾讯", Value = "qq" });persons.Add(new SelectListItem { Text = "如鹏", Value = "rupeng", Selected = true });ViewBag.persons = persons;ViewBag.persons1 = new string[] { };return View();}

  

Cshtml如下:
@Html.DropDownList("persons1", (IEnumerable<SelectListItem>)ViewBag.persons)

生成的html中第二条数据照样不会被selected

不知道微软为什么把DropDownList这么简单的一个东西搞的这么复杂,正验证了这句话“写的越多,错的越多”。当然也许微软会给出理由说我们用错了,说“It’s not a bug,It’s a feature,by design”好吧!谢特!




原标题:ASP.Net MVC的ViewBag一个坑,不要跳进去

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

COA证书:https://www.goluckyvip.com/tag/17139.html
苹果发布声明:https://www.goluckyvip.com/tag/1714.html
Cobiro:https://www.goluckyvip.com/tag/17140.html
Cobone:https://www.goluckyvip.com/tag/17141.html
Cobone开店:https://www.goluckyvip.com/tag/17142.html
cockpitlab:https://www.goluckyvip.com/tag/17143.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
5月贾汪好玩的地方 贾汪哪有好玩的地方:https://www.vstour.cn/a/363179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流