asp.net mvc dropdownlist否ViewData项 [英] asp.net mvc dropdownlist no ViewData item
问题描述
我开始研究mvc,尝试添加下拉列表,进行构建
<%= Html.DropDownList(ddl )%>
但显示错误
没有具有ddl键的IEnumerable类型的ViewData项目
为什么?我使用简单的代码,只传递名称参数,所以为什么错误?
没有IEnumerable类型的ViewData项目
这意味着帮助程序期待一个项目键入'IEnumerable'
,例如, List<>
与Id 'ddl' / code>
如果您尝试显示一个DropDownList,它具有来自某些静态源的项目,这里是一种方法。
//创建新的IEnummerable
列表< string> ddl =
new List< string>(new [] {item1,item2};
//添加项
ddl.Add(Item1);
ddl.Add(Item2);
//返回View现在保存ddl
ViewData [ddl] = ddl;
return View() ;
有关更多见解,请查看
i start to study mvc, try to add dropdownlist, make construction
<%= Html.DropDownList("ddl") %>
but it show error
There is no ViewData item of type 'IEnumerable' that has the key 'ddl'
why? i use simple code, only pass name parameter, so why the error?
"There is no ViewData item of type 'IEnumerable'"
It means that helper method is expecting an item of type 'IEnumerable'
for e.g, List<>
with an Id 'ddl'
If you are trying to display a DropDownList which has items from some static source, here is one way to do this.
// create new IEnummerable
List<string> ddl =
new List<string>(new [] {"item1","item2" };
// add Items
ddl.Add("Item1");
ddl.Add("Item2");
// return View which holds ddl now
ViewData["ddl "] = ddl ;
return View();
For more insight have a look at
这篇关于asp.net mvc dropdownlist否ViewData项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!