Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble getting DropDownListFor to work on EditorFor in MVC3

I've got a pretty simple problem that has a solution I'm not able to find.

Given the following model:

public class InfoViewModel
{
    public SelectList States { get; set; }

    [DisplayName("State")]
    public string State { get; set; }
}

The following works fine in my view:

@Html.DropDownListFor(m => m.State, Model.States)

However, if I try to pull this into an editor template (named "SelectList"), such as:

@model System.String
@Html.DropDownListFor(m => m, ViewData["selectList"])

And then use EditorFor to build the dropdown list:

@Html.EditorFor(m => m.State, "SelectList", new { selectList = Model.States })

It fails with the following error:

'System.Web.Mvc.HtmlHelper<string>' does not contain a definition for 
'DropDownListFor' and the best extension method overload 
'System.Web.Mvc.Html.SelectExtensions.DropDownListFor<TModel,TProperty>
(System.Web.Mvc.HtmlHelper<TModel>, 
System.Linq.Expressions.Expression<System.Func<TModel,TProperty>>, 
System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>)' 
has some invalid arguments

I'm having a hard time understanding the difference between these two. I've tried various workarounds to troubleshoot, and either get the same error, or something else.

Thanks in advance.

like image 967
Jerad Rose Avatar asked Jun 12 '11 01:06

Jerad Rose


1 Answers

There is no such overload:

@Html.DropDownListFor(m => m, ViewData["selectList"])

The second parameter of the DropDownListFor helper musty be a SelectList. So:

@Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"])
like image 63
Darin Dimitrov Avatar answered Sep 18 '22 13:09

Darin Dimitrov