Home Using 5th overload of Html.DropDownList causes exception. (does not contain a definition for 'DropDownList' and the best extension method overload..)
Reply: 1

Using 5th overload of Html.DropDownList causes exception. (does not contain a definition for 'DropDownList' and the best extension method overload..)

Jc Balantakbo
1#
Jc Balantakbo Published in 2017-09-11 11:37:38Z

I have a model with this property

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

And in my view I have this on my view which causes the error

error CS1928: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'DropDownList' and the best extension method overload '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" })

I am trying to use the this overload

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

I can't seem to figure out what's wrong with this code. I believe I have followed the overload properly.

I have also tried casting it. I still doesn't work though.

@Html.DropDownList("ResponseTypeId", "--Select Response Type--", (IEnumerable<SelectListItem>)question.ResponseTypes, new { @class = "form-control responseType", @id = "cbxType" })
Stephen Muecke
2#
Stephen Muecke Reply to 2017-09-11 12:02:19Z

Your using a method in the System.Web.WebPages.Html namespace, not the System.Web.Mvc namespace.

What you need to use is this overload where the signature is

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

so the code in your view will be

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

however I recommend you use the strongly typed

@Html.DropDownListFor(m => m.ResponseTypeId, question.ResponseTypes, "--Select Response Type--", new { @class = "form-control responseType", id = "cbxType" })
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.415387 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO