在 MVC3 中使 DropDownListFor 在 EditorFor 上工作时出现问题
我有一个非常简单的问题,但我找不到解决方案。
给出以下模型:
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
我很难理解这两者之间的区别。我尝试了各种解决方法来排除故障,但要么出现相同的错误,要么出现其他错误。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在这样的重载:
DropDownListFor
帮助器的第二个参数必须是SelectList
。所以:There is no such overload:
The second parameter of the
DropDownListFor
helper musty be aSelectList
. So: