Chuck Niddy
Chuck Niddy

Reputation: 13

How do you add a dropdown list in asp.net mvc with dropdownlistfor?

I am trying to add a drop down list to my view using DropDownListFor and I am not sure what I am doing wrong.

My models:

public class Catalog
{
    public Catalog()
    {
        this.Locations = new HashSet<Location>();
        this.Categories = new HashSet<Category>();
        this.SubCategories = new HashSet<SubCategory>();
    }

    [Key]
    public int CatalogID { get; set; }
    public int BrandID { get; set; }
    public int WarrantyPDFID { get; set; }

    public string Name { get; set; }
    public string PDF { get; set; }

    public bool Active { get; set; }
    public DateTime CreatedOn { get; set; }
    public int CreatedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
    public int ModifiedBy { get; set; }
    public bool SoftDelete { get; set; }

    public virtual ICollection<Location> Locations { get; set; }
    public virtual ICollection<Category> Categories { get; set; }
    public virtual ICollection<SubCategory> SubCategories { get; set; }
    public virtual Brand Brand { get; set; }
    public virtual WarrantyPDF WarrantyPDF { get; set; }
}

public class Brand
{
    [Key]
    public int BrandID { get; set; }
    public string Name { get; set; }
    public int Sort { get; set; }

    public bool Active { get; set; }
    public DateTime CreatedOn { get; set; }
    public int CreatedBy { get; set; }
    public DateTime ModifiedOn { get; set; }
    public int ModifiedBy { get; set; }
    public bool SoftDelete { get; set; }
}

Controller Add function (HttpGet)

public ActionResult AddCatalog()
    {
        Context _db = new Context();

        Catalog catalog = new Catalog();

        List<SelectListItem> brands = new List<SelectListItem>();

        foreach(var brand in _db.Brands)
        {
            var item = new SelectListItem
            {
                Value = brand.BrandID.ToString(),
                Text = brand.Name
            };

            brands.Add(item);
        }

        ViewBag.Brands = brands;

        return View(catalog);
    }

Relevant part of my view

@model App.Models.Catalog

@{
    ViewBag.Title = "AddCatalog";
    Layout = "~/Views/LayoutAdmin.cshtml";
}

<h2>Add Catalog</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
       <h4>Catalog</h4>
       <hr />
       @Html.ValidationSummary(true, "", new { @class = "text-danger" })
       <div class="form-group">
           Brand 
           <div class="col-md-10">
               @Html.DropDownListFor(m => m.BrandID, ViewBag.Brands, "Select Brand", null)
               @Html.ValidationMessageFor(model => model.BrandID, "", new { @class = "text-danger" })
            </div>
        </div>

When I try to load the page I am getting this error. Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Upvotes: 0

Views: 1090

Answers (1)

Shyju
Shyju

Reputation: 218702

The expression ViewBag.Brands is of type dynamic. The overload of DropDownListFor helper method you are using, expects a collection of SelectListItem as the second parameter. So you need to cast the data you stored in ViewBag.Brands to a collection of SelectListItems

This should work.

@Html.DropDownListFor(m => m.BrandID, (IEnumerable<SelectListItem>)ViewBag.Brands ,
                                                                  "Select Brand", null)

or using the as safe cast operator

@Html.DropDownListFor(m => m.BrandID, ViewBag.Brands as IEnumerable<SelectListItem>,
                                                                 "Select Brand", null)

Upvotes: 1

Related Questions