如鵬的學習管理系統是使用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, allowMultPRivate 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”好吧!謝特!
新聞熱點
疑難解答