开发者

Trouble getting DropDownListFor to work on EditorFor in MVC3

开发者 https://www.devze.com 2023-03-12 21:02 出处:网络
I\'ve got a pretty simple problem that has a solution I\'m not able to find. Given the following model:

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 inva开发者_如何学Golid 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.


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"])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号