简体   繁体   中英

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

I am trying to build a form using Razor template engine with ASP.NET MVC 5.

I have the following encapsulated call which I pass to the main view

public class ManagePresenter
{

    public ActionFormViewModel ActionForm { get; set; }

    public List<SelectListItem> Users { get; set; }
}

Then I have this view

@using(Html.BeginForm("Index", "Manage", FormMethod.Post, new { @class = "form-inline", Id = "ManageEmployeeForm" }))
{

    Html.AntiForgeryToken();
    Html.HiddenFor(m => m.ActionForm.From);
    Html.HiddenFor(m => m.ActionForm.To);

    <div class="form-group">

    @{
        Html.LabelFor(m => m.ActionForm.UserId, new { @class = "sr-only" });
        Html.DropDownListFor(x => x.ActionForm.UserId, Model.Users, new { @Class = "form-control" });
        Html.ValidationMessageFor(m => m.ActionForm.UserId);
    }

    </div>

    <div class="checkbox">
        <label>
            <label>
                @Html.CheckBoxFor(m => m.ActionForm.ShowAlternateEntries, new { Title = "Alternate entries are entries from the telephony system that will help you compare against manual entries" })
            </label>
        </label>
    </div>

    <button type="submit" class="btn btn-primary" id="ManageEmployeeGetTime">View Records</button>

}

but every time I run my application I get the following error

CS1928: 'System.Web.Mvc.HtmlHelper' 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.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object)' has some invalid arguments

What could be causing this error? Model.Users is a List<SelectListItem>

What am I missing here?

The error means (in your case) that one of the arguments of your DropDownListFor() method is invalid, and logically this can only be the 2nd parameter (for IEnumerable<SelectListItem> ).

You have claimed that the model contains a property public List<SelectListItem> Users { get; set; } public List<SelectListItem> Users { get; set; } public List<SelectListItem> Users { get; set; } so either you have created you own class named SelectListItem or you have included a reference to System.Web.WebPages.Html and are using the SelectListItem class of that assembly rather than the SelectListItem class of System.Web.Mvc .

Generally, you should not need System.Web.WebPages.Html , but if for some reason you do, then use the fully qualified name of class in you model

public List<System.Web.Mv.SelectListItem> Users { get; set; }

As a side note, your view will not generate any form controls (except the ckeckbox and submit button). Your have not prefixed the HtmlHelper methods with @ which means the method will be executed, but it results will not be output in the view. You need to change the view to

@using(Html.BeginForm("Index", "Manage", FormMethod.Post, new { @class = "form-inline", Id = "ManageEmployeeForm" }))
{
    @Html.AntiForgeryToken();
    @Html.HiddenFor(m => m.ActionForm.From);
    @Html.HiddenFor(m => m.ActionForm.To);
    <div class="form-group">
        @Html.LabelFor(m => m.ActionForm.UserId, new { @class = "sr-only" });
        @Html.DropDownListFor(x => x.ActionForm.UserId, Model.Users, new { @class = "form-control" });
        @Html.ValidationMessageFor(m => m.ActionForm.UserId);
    </div>
    ....

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