如鵬的學(xué)習(xí)管理系統(tǒng)是使用asp.net MVC 5開發(fā)的,今天一個(gè)新版本發(fā)布后網(wǎng)站出現(xiàn)一個(gè)Bug,學(xué)生在下拉列表中選中的項(xiàng)再加載顯示的時(shí)候發(fā)現(xiàn)仍然沒被選中。詳細(xì)一點(diǎn)說吧:
假如有這樣一個(gè)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>
竟然第二項(xiàng)沒有處于選中狀態(tài),太詭異了吧!
只要把DropDownList第二個(gè)參數(shù)的"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擴(kuò)展類中,DropDownList方法最終是調(diào)用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>()集合被當(dāng)成defaultValue參數(shù)傳遞給GetSelectListWithDefaultValue方法了(why?),在方法內(nèi)部又把defaultValue給 Convert.ToString()一下,變成了”System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]”這么一個(gè)玩意, GetSelectListWithDefaultValue的主要邏輯就是查找selectList中等于”System.Collections.Generic.List`1[System.Web.Mvc.SelectListItem]”的值,能找到才算見了鬼呢!!!
經(jīng)過上面的分析我們還可以知道,不能讓cshtml中DropDownList的第一個(gè)name參數(shù)和ViewBag中任何一個(gè)屬性重名,否則還是會有問題,比如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中第二條數(shù)據(jù)照樣不會被selected
不知道微軟為什么把DropDownList這么簡單的一個(gè)東西搞的這么復(fù)雜,正驗(yàn)證了這句話“寫的越多,錯(cuò)的越多”。當(dāng)然也許微軟會給出理由說我們用錯(cuò)了,說“It’s not a bug,It’s a feature,by design”好吧!謝特!
新聞熱點(diǎn)
疑難解答
圖片精選