无法让DropDownListFor在MVC3中使用EditorFor

时间:2011-06-12 01:48:28

标签: asp.net-mvc asp.net-mvc-3 razor editorfor html.dropdownlistfor

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

鉴于以下模型:

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

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

在我看来,以下工作正常:

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

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

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

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

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

失败并出现以下错误:

'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

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

提前致谢。

1 个答案:

答案 0 :(得分:13)

没有这样的过载:

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

DropDownListFor助手的第二个参数必须是SelectList。所以:

@Html.DropDownListFor(m => m, (SelectList)ViewData["selectList"])
相关问题