问题描述
我建设我的第一个MVC应用程序经过多年做Web表单,以及由于某种原因,我不能做这项工作:
I'm building my first MVC application after years of doing webforms, and for some reason I am not able to make this work:
@Html.DropDownList("PriorityID", String.Empty, new {@class="textbox"} )
错误消息:
System.Web.Mvc.HtmlHelper< SPDR.Models.Bug>
'不包含一个定义的DropDownList
和最佳推广方法重载 System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper,字符串System.Collections.Generic.IEnumerable< System.Web.Mvc.SelectListItem&GT ;,对象)
有一些无效参数
System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>
' does not contain a definition for DropDownList
and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object)
has some invalid arguments
任何帮助非常AP preciated!
Any help greatly appreciated!
推荐答案
综观控制器和式学习更多的关于MVC是如何工作,我能够理解这一点。
Looking at the controller, and learing a bit more about how MVC actually works, I was able to make sense of this.
我的看法是自动生成的人之一,包含该行code的:
My view was one of the auto-generated ones, and contained this line of code:
@Html.DropDownList("PriorityID", string.Empty)
要添加HTML属性,我需要做的是这样的:
To add html attributes, I needed to do something like this:
@Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { @class="dropdown" })
再次感谢@Laurent您的帮助,我才意识到这个问题并没有明确的,因为它可能是...
Thanks again to @Laurent for your help, I realise the question wasn't as clear as it could have been...
更新:
这样做的一个更好的办法是使用DropDownListFor在可能的情况,这样你不依赖于一个神奇的字符串的name属性。
A better way of doing this would be to use DropDownListFor where possible, that way you don't rely on a magic string for the name attribute
@Html.DropDownListFor(x => x.PriorityID, (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { @class = "dropdown" })
这篇关于添加CSS类选择使用@ Html.DropDownList()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!