내 컨트롤러에서 SelectListItems의 목록을 만들고이를 ViewData에 저장합니다. View에서 ViewData를 읽을 때 잘못된 유형에 대한 오류가 발생합니다. 수동으로 유형을 변환하면 작동하지만 자동으로 수행되어야합니다. 누군가 설명 할 수 있습니까?왜 Html.DropDownListFor가 여분의 캐스트가 필요합니까?
컨트롤러 :
enum TitleEnum { Mr, Ms, Mrs, Dr };
var titles = new List<SelectListItem>();
foreach(var t in Enum.GetValues(typeof(TitleEnum)))
titles.Add(new SelectListItem()
{ Value = t.ToString(), Text = t.ToString() });
ViewData["TitleList"] = titles;
보기 : 만약 내가 올바르게 기억
// Doesn't work
Html.DropDownListFor(x => x.Title, ViewData["TitleList"])
// This Works
Html.DropDownListFor(x => x.Title, (List<SelectListItem>) ViewData["TitleList"])
그냥보기 : @ Html.DropDownList ("TitleList")'보기에서 추가 캐스팅없이 작동합니다. – Arvis