1

I need to use jQuery to do some validation on a DropDownList. Therefore I am trying to add a htmlAttribute like this:

@Html.DropDownList("category_id", "Vælg..", new { @class = "required" })

I am getting the following errors:

Error   2   'System.Web.Mvc.HtmlHelper<MvcApplication3.Models.Question>' 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>, string)' has some invalid arguments   c:\Users\Kenan\Documents\Visual Studio 2010\Projects\MvcApplication3 - Copy\MvcApplication3\Views\AdminQuestion\GridQuestion.cshtml 38  14  MvcApplication3
Error   3   Argument 3: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>' c:\Users\Kenan\Documents\Visual Studio 2010\Projects\MvcApplication3 - Copy\MvcApplication3\Views\AdminQuestion\GridQuestion.cshtml 38  47  MvcApplication3
Error   4   Argument 4: cannot convert from 'AnonymousType#1' to 'string'   c:\Users\Kenan\Documents\Visual Studio 2010\Projects\MvcApplication3 - Copy\MvcApplication3\Views\AdminQuestion\GridQuestion.cshtml 38  57  MvcApplication3

If I change the code to:

@Html.DropDownList("category_id", null, new { @class = "required " })

It works, but without a default value, which is not what I want.

What am I doing wrong?

1 Answer 1

2

You'll notice in the Overload List that there's no overload for string, string, object.

The overload you may be looking for is:

DropDownList(HtmlHelper, String, IEnumerable<SelectListItem>, Object)

You'd write this in your view as:

@Html.DropDownList("SomeString", MyEnumerable, new {@class = "required"}

The reason your second example works, i.e. string, null, object is because IEnumerable<T> is nullable.

UPDATE

You may find that DropDownListFor is a better match for what you need.

The exact overload you'll probably want is:

HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, IEnumerable<SelectListItem>, Object

implimented as:

@Html.DropDownListFor(m => m.category_id, ViewBag.category_id, new {@class = "required"})
Sign up to request clarification or add additional context in comments.

12 Comments

What should i write in the HtmlHelper parameter?
That's already covered by the @Html bit you're using. It's the same as other extension methods where the first parameter is specificed as (this HtmlHelper,...). So you can ignore that parameter knowing it's covered by your @Html part.
I am getting the SelectList from the ViewBag. It is called ViewBag.category_id - I have changed the code to this: @Html.DropDownList("Kategorier", ViewBag.category_id, new { @class = "required" } ) but i am getting this error:
Error 1 'System.Web.Mvc.HtmlHelper<MvcApplication3.Models.Question>' has no applicable method named 'DropDownList' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. c:\Users\Kenan\Documents\Visual Studio 2010\Projects\MvcApplication3 - Copy\MvcApplication3\Views\AdminQuestion\GridQuestion.cshtml 38 10 MvcApplication3
What is category_id ??? If it's an int, there's no overload for string, int, object. Does this make sense?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.