2017-09-11 100 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

之前,這也可以工作 –

相關問題