0
  • Controller Code:

    List Months = new List();

            Months.Add("Jan");
            Months.Add("Feb");
    
        ViewData["Months"] = new SelectList(Months);
    
  • View Code:

${Html.DropDownList((SelectList)ViewData["Months"])}

Error: Argument 2: cannot convert from 'System.Web.Mvc.SelectList' to 'string'

zero323
  • 322,348
  • 103
  • 959
  • 935
webtester
  • 21
  • 6

1 Answers1

0

The error is pretty clear, it states that it expects a type of string and you are passing a SelectList make sure you use the correct overloads.

@Html.DropDownList("Months",selectList: ((SelectList)ViewData["Months"]))
Syneryx
  • 1,278
  • 11
  • 18
  • I tried this but still getting the error : '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, string)' has some invalid arguments error CS1503: Argument 3: cannot convert from 'object' to 'string''System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' has some invalid arguments error CS1503: Argument 3: cannot convert from 'object' to 'string' – webtester Dec 28 '12 at 11:01
  • I unfortunately do not have the mvc-spark view engine installed. but i'm assuming that you are still not using the correct overloads. try to find one which allows you to pass a SelectList(). – Syneryx Dec 28 '12 at 11:06
  • As per my understanding dropDownList do accept(System.Web.Mvc.HtmlHe‌​lper, string) as the parameters and in our example ViewData acts as kind of HtmlHelper. – webtester Dec 28 '12 at 11:11
  • still Not working...Error: Cannot convert from 'object' to 'string' – webtester Dec 28 '12 at 11:24