MVC3的DropDownList + ViewBag问题 [英] MVC3 DropDownList + ViewBag issue
本文介绍了MVC3的DropDownList + ViewBag问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这code正常工作
List<StateModelView> stateList = (from x in db.States
select new StateModelView {
ID = x.ID,
StateName = x.StateName
}).OrderBy(w => w.StateName).ToList();
ViewBag.StateList = new SelectList(stateList, "ID", "StateName");
在HTML我有
@Html.DropDownList("StateList", ViewBag.StateList)
无论如何,我得到了错误
Anyway I got the error
CS1973:'System.Web.Mvc.HtmlHelper'有一个名为DropDownList的没有适用的方法,但似乎有这个名字的扩展方法。扩展方法不能进行动态调度。考虑强制转换动态参数或调用没有扩展方法的语法扩展方法。
我怎样才能解决这个问题?
How I can resolve it?
推荐答案
的 ViewBag
是的 动态
对象,它不能从您的视图直接使用(这基本上是错误的说法) 。你需要转换它:
The ViewBag
is a dynamic
object, which cannot be used directly from your View (that is basically what the error is saying). You'll need to cast it:
@Html.DropDownList("StateList", (SelectList) ViewBag.StateList)
另一种选择是使用的ViewData code>尽管它可能还需要铸造。
Another option would be to use ViewData
though it may also require casting.
这篇关于MVC3的DropDownList + ViewBag问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文