【问题标题】:How do you add a dropdown list in asp.net mvc with dropdownlistfor?如何使用 dropdownlistfor 在 asp.net mvc 中添加下拉列表?
【发布时间】:2018-02-03 00:55:23
【问题描述】:

我正在尝试使用 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”的适用方法,但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或调用扩展方法而不使用扩展方法语法。

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

表达式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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    相关资源
    最近更新 更多