2017-09-11 102 views
1

我有这个属性使用Html.DropDownList的第5个重载会导致异常。 (不包含“DropDownList的”和最佳推广方法重载的定义。)

public List<System.Web.Mvc.SelectListItem> ResponseTypes { get; set; } 

在我看来,我有这个在我看来,这将导致错误的模型

错误CS1928:'System.Web.Mvc.HtmlHelper'不包含'DropDownList'的定义和最佳扩展方法重载'System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper ,string

@Html.DropDownList("ResponseTypeId", "--Select Response Type--", question.ResponseTypes, new { @class = "form-control responseType", @id = "cbxType" }) 

我想使用此重载

public IHtmlString DropDownList(string name, string defaultOption, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes); 

我似乎无法找出什么不对的代码。我相信我已经适当地跟踪了过载。

我也试过铸造它。我仍然无法工作。

@Html.DropDownList("ResponseTypeId", "--Select Response Type--", (IEnumerable<SelectListItem>)question.ResponseTypes, new { @class = "form-control responseType", @id = "cbxType" }) 
+1

是不是过载....名称,列表,默认条目?把默认条目前的选择列表.....似乎对我的看法确定。 – Wheels73

+0

你似乎是对的。但我不知道为什么intellisense告诉我默认值在我认为的listItems –

回答

2

您使用的System.Web.WebPages.Html命名空间,而不是System.Web.Mvc命名空间的方法。

你需要使用什么是this overload其中签名是

public static MvcHtmlString DropDownList(
    this HtmlHelper htmlHelper, 
    string name, 
    IEnumerable<SelectListItem> selectList, 
    string optionLabel, 
    object htmlAttributes 
) 

所以在您的视图代码会

@Html.DropDownList("ResponseTypeId", question.ResponseTypes, "--Select Response Type--", new { @class = "form-control responseType", id = "cbxType" }) 

不过我建议你使用强类型

@Html.DropDownListFor(m => m.ResponseTypeId, question.ResponseTypes, "--Select Response Type--", new { @class = "form-control responseType", id = "cbxType" }) 
+0

之前,这也可以工作 –

相关问题