0
votes

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?

1
@JamesP please, show the basis for your statement, because all definitions of DropDownListFor I found accept IEnumerable<SelectListItem>. - kiziu
wild guess, are you sure you are not mixing MVC versions? Maybe SelectListItem in the model is from a different version of MVC? - kiziu
@kiziu yes I am sure. infactI am using the something in a different view just with a different database and it is working fine. - Jaylen
Similarly, your Html.AntiForgeryToken(); needs to be @Html.AntiForgeryToken() (and ditto for the hidden inputs - user3559349
The only reasonable explanation is that you have mistakenly included the wrong assembly and your using System.Web.WebPages.Html.SelectListItem (or you have created your own class) instead of the required System.Web.Mvc.SelectListItem - user3559349

1 Answers

1
votes

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