System.Web.Mvc。HtmlHelper不包含'DropDownListFor'的定义

时间:2021-05-23 16:53:27

I am getting a run time exception as shown below, compiling the source code doesn't show any issues. The issue is not happening for @html.TextBoxFor(), why is this happening for 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> 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

System.Web.Mvc.HtmlHelper < EApp.ViewModels。AdminViewModel>不包含DropDownListFor的定义和最佳扩展方法重载系统。web.mvc.html . selectextensions.dropdownlistfor (System.Web.Mvc)。HtmlHelper < TModel >,System.Linq.Expressions.Expression <系统。有一些无效的参数,例如:func >, System.Collections.Generic.IEnumerable )。 ,tproperty> ,tproperty>

2 个解决方案

#1


2  

This error can be caused by using the wrong SelectListItem type. I inadvertently selected the wrong namespace when I hit Ctrl-. on my unresolved SelectListItem type declaration. I selected System.Web.WebPages.Html instead of System.Web.Mvc. This resulted in the semi-cryptic run-time error in the View. The solution is to make sure you are using the correct SelectListItem type in your Controller, ViewModel, etc., by either using the correct namespace in your using statement, or by fully qualifying the SelectListItem type, if needed. (Hopefully it is not needed...)

这个错误可以由使用错误的SelectListItem类型引起。我在按Ctrl键时无意中选择了错误的名称空间。在未解决的SelectListItem类型声明中。我选择System.Web.WebPages。Html而不是System.Web.Mvc。这导致了视图中的半隐式运行时错误。解决方案是确保在控制器、ViewModel等中使用正确的SelectListItem类型,方法是在using语句中使用正确的名称空间,或者在需要时完全限定SelectListItem类型。(希望不需要……)

#2


0  

A bit late I know, but how about dropping the '@' in front of Model.EView.EStatus?

我知道有点晚了,但是把“@”放到Model.EView.EStatus怎么样?

So:

所以:

@Html.DropDownListFor(m => m.EView.EStatusId, Model.EView.EStatus)

#1


2  

This error can be caused by using the wrong SelectListItem type. I inadvertently selected the wrong namespace when I hit Ctrl-. on my unresolved SelectListItem type declaration. I selected System.Web.WebPages.Html instead of System.Web.Mvc. This resulted in the semi-cryptic run-time error in the View. The solution is to make sure you are using the correct SelectListItem type in your Controller, ViewModel, etc., by either using the correct namespace in your using statement, or by fully qualifying the SelectListItem type, if needed. (Hopefully it is not needed...)

这个错误可以由使用错误的SelectListItem类型引起。我在按Ctrl键时无意中选择了错误的名称空间。在未解决的SelectListItem类型声明中。我选择System.Web.WebPages。Html而不是System.Web.Mvc。这导致了视图中的半隐式运行时错误。解决方案是确保在控制器、ViewModel等中使用正确的SelectListItem类型,方法是在using语句中使用正确的名称空间,或者在需要时完全限定SelectListItem类型。(希望不需要……)

#2


0  

A bit late I know, but how about dropping the '@' in front of Model.EView.EStatus?

我知道有点晚了,但是把“@”放到Model.EView.EStatus怎么样?

So:

所以:

@Html.DropDownListFor(m => m.EView.EStatusId, Model.EView.EStatus)