c# - System.Web.Mvc.HtmlHelper 不包含 'DropDownListFor' 的定义

标签 c# asp.net-mvc linq asp.net-mvc-4 dropdownlistfor

我收到如下所示的运行时异常,编译源代码没有显示任何问题。 @html.TextBoxFor() 没有出现此问题,为什么 DropDownListFor 会出现这种情况?

@Html.DropDownListFor(m => m.EView.EStatusId, @Model.EView.EStatus)
public class EView
{
    #region Constructor

    public EView()
    {
        this.EDataView = new EDataViewModel();
    }

    #endregion

    #region Properties

    public int EStatusId { get; set; }

    public EDataViewModel EDataView { get; set; }

    public IEnumerable<SelectListItem>  EStatus
    {
        get {
            SelectListItem item = null;
            List<SelectListItem> status = null;

            status = new List<SelectListItem>();
            item = new SelectListItem();

            item.Text = "Open";
            item.Value = "1";
            status.Add(item);

            item = new SelectListItem();

            item.Text = "Closed";
            item.Value = "2";
            status.Add(item);
            return status;
        }
    }

    #endregion
}

System.Web.Mvc.HtmlHelper<EApp.ViewModels.AdminViewModel>不包含 DropDownListFor 的定义和最好的扩展方法重载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>)有一些无效参数

最佳答案

此错误可能是由于使用错误的 SelectListItem 类型引起的。当我按下 Ctrl- 时,我无意中选择了错误的命名空间。关于我 Unresolved SelectListItem 类型声明。我选择了 System.Web.WebPages.Html 而不是 System.Web.Mvc。这导致 View 中出现半神秘的运行时错误。解决方案是确保您在 Controller、ViewModel 等中使用正确的 SelectListItem 类型,方法是在 using 语句中使用正确的命名空间,或者根据需要完全限定 SelectListItem 类型。 (希望不需要......)

关于c# - System.Web.Mvc.HtmlHelper 不包含 'DropDownListFor' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27793377/

相关文章:

c# - LINQ 中怎么写呢?

c# - 我是否使用了错误的设置/获取功能?

Azure 服务的 C# 授权过滤器?

c# - windows phone 8中如何监听scrollviewer的滚动事件?

asp.net-mvc - 我正在寻找有关自定义 MVC3 编辑器模板的高级资源

Linq 2 Sybase ASE 数据库?有什么选择?

c# - realm.xamarin 抛出错误 : The method 'Contains' is not supported. 与 'Any' 相同

asp.net - 当未提供路由名称时,使用 RouteLink 生成 url 的方法是什么?

asp.net-mvc - 更新部署文件后 Antlr3.runtime 访问被拒绝

c# - LINQ 按数字序列排序