简体   繁体   中英

System.Web.Mvc.HtmlHelper does not contain a definition for 'DropDownListFor'

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.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

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...)

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

So:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM