Trouble getting DropDownListFor to work on EditorFor in MVC3
10,449
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"])
Author by
Jerad Rose
C#, MVC, and React web developer. Developer at Kaggle. Founder of Animal Crossing Community. My Stack Overflow resume.
Updated on June 14, 2022Comments
-
Jerad Rose almost 2 years
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.
-
Jerad Rose almost 13 yearsThanks, Darin, that was exactly it. I should have realized that.