无法让DropDownListFor在MVC3中使用EditorFor

Jer*_*ose 9 asp.net-mvc editorfor html.dropdownlistfor razor asp.net-mvc-3

我有一个非常简单的问题,有一个我无法找到的解决方案.

鉴于以下模型:

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

    [DisplayName("State")]
    public string State { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我认为以下工作正常:

@Html.DropDownListFor(m => m.State, Model.States)
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将其拉入编辑器模板(名为"SelectList"),例如:

@model System.String
@Html.DropDownListFor(m => m, ViewData["selectList"])
Run Code Online (Sandbox Code Playgroud)

然后使用EditorFor构建下拉列表:

@Html.EditorFor(m => m.State, "SelectList", new { selectList = Model.States })
Run Code Online (Sandbox Code Playgroud)

它失败并出现以下错误:

'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
Run Code Online (Sandbox Code Playgroud)

我很难理解这两者之间的区别.我已尝试各种解决方法来排除故障,并获得相同的错误,或其他.

提前致谢.

Dar*_*rov 13

没有这样的过载:

@Html.DropDownListFor(m => m, ViewData["selectList"])
Run Code Online (Sandbox Code Playgroud)

DropDownListFor帮手的第二个参数必须是a SelectList.所以:

@Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"])
Run Code Online (Sandbox Code Playgroud)