如何使用 dropdownlistfor 在 asp.net mvc 中添加下拉列表?

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

我正在尝试使用 DropDownListFor 将下拉列表添加到我的视图中,但我不确定我做错了什么。

我的模特:

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

控制器添加函数(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);
    }

我认为的相关部分

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

当我尝试加载页面时出现此错误。 编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper' 没有名为 'DropDownListFor' 的适用方法,但似乎具有该名称的扩展方法。不能动态调度扩展方法。考虑在没有扩展方法语法的情况下转换动态参数或调用扩展方法。

表达式 ViewBag.Brands 的类型为 dynamic。您正在使用的 DropDownListFor 辅助方法的重载需要 SelectListItem 的集合作为第二个参数。所以你需要将你存储在 ViewBag.Brands 中的数据转换为 SelectListItems

的集合

这应该有效。

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

或使用 as 安全转换运算符

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