【问题标题】:Dynamically Category wise subcategory's dropdown list动态分类子类别的下拉列表
【发布时间】:2020-10-05 19:39:17
【问题描述】:

我想创建一个动态的单独的产品相关类别明智子类别的下拉列表并单独显示产品。我已经创建了一个动态的分类产品。但我不明白我将如何创建类别明智的动态子类别。我已经在类别、子类别和产品模型之间建立了关系。

这是我的代码:

型号

//Product's model

 public class Shop
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "product name")]
        public String Name { get; set; }
        [Required]
        public int Price { get; set; }


        public String Image { get; set; }
        public String Image1 { get; set; }

        public List<Photo1> Photos { get; set; }

        [Required]
        public int Quantity { get; set; }

        [Required]
        public bool IsAvailable { get; set; }

        [Display(Name = "Category")]

        public int? CategoryTypeId { get; set; }

        [ForeignKey("CategoryTypeId")]
        public Category Category { get; set; }

        [Display(Name = "SubCategory")]

        public int? SubCategoryTypeId { get; set; }

        [ForeignKey("SubCategoryTypeId")]
        public SubCategory SubCategory { get; set; }
    }


//SubCategory Model

  public class SubCategory
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "SubCategory Name")]
        public string SubCategoryName { get; set; }

        public int CategoryID { get; set; }

        [JsonIgnore]
        public Category Category { get; set; }
    }

//Category Model

 public class Category
    {
        public int Id { get; set; }

        [Required]
        [Display(Name = "Category Name")]
        public string CategoryName { get; set; }

        public ICollection<SubCategory> SubCategories { get; set; }
    }

组件

  public class CategoryWiseMenu:ViewComponent
    {
       private readonly ApplicationDbContext _db;

        public CategoryWiseMenu(ApplicationDbContext db)
        {
            _db = db;
        }

        public IViewComponentResult Invoke()
        {
            var c = _db.Category.OrderBy(p => p.CategoryName);
            return View(c);
        }
    }

默认.cshtml



<ul class="navbar-nav flex-grow-1">


    @foreach (var category in Model)
    {
        <li class="nav-item text-dark">
            <a asp-controller="ShopShow" asp-action="ListCategories"
               asp-route-cate="@category.CategoryName" class="nav-link text-dark">@category.CategoryName</a>
        </li>
    }
    
</ul>

ShopShow 控制器及其视图

//Controller

  public IActionResult ListCategories(string cate)
        {
            var c = _db.Shop.Where(x => x.Category.CategoryName == cate).ToList();
            ViewBag.t = c;
            return View();
        }



  //....................//
 //ListCagories.cshtml//
//..................//


@model DigitalShop.Models.Shop



<h1 class="text-center text-danger">Buy Now!!</h1>
<br /><br />
<div class="row">

    @foreach (var laptop in ViewBag.t)
    {
        <div class="col-4 ml-5">
            <div class="card mb-4">
                <div class="card-header">
                    <h4 class="my-4 font-weight-normal">
                        <label style="font-size:23px; color:black;text-align:center">@laptop.Name</label>
                    </h4>
                </div>
                <img src="~/@laptop.Image" alt="Card Image" class="card-img-top" style="height:200px;" />

                @*@if (laptop.Photos != null && laptop.Photos.Count != 0)
                    {
                        <img src="~/@laptop.Photos[0].Image" alt="Card Image" class="card-img-top" style="height:200px;" />
                    }*@

                @*<video src="~/@laptop.Image1" alt="Card Image" class="card-img-top" controls height="300px" loop />*@

                <div class="card-header">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="btn-group">
                            <label style="font-size:20px;color:darkblue"><b>Price:@laptop.Price</b></label>
                        </div>


                        <a asp-action="Details" asp-controller="ShopShow" asp-route-id="@laptop.Id" class="btn btn-primary pull-right btn-outline-light">Details</a>
                    </div>
                </div>
            </div>
        </div>

    }
</div>



输出:

我已经在布局页面中使用了@(await Component.InvokeAsync("CategoryWiseMenu"))。在上面的输出中,我成功地创建了一个动态分类的产品。但我想要动态单独的产品相关类别明智子类别的下拉列表。有什么解决办法。

【问题讨论】:

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


    【解决方案1】:

    使用 categoryName 从 db 中获取商店,然后从商店中获取子类别。

      //pass CategoryName 
    
         public IActionResult ListSubCategories(string cate)
                {
                    //get shops with subcategory related to categoryName
                    var c = _db.Shop.Include(c => c.Category.Where(x => x.Category.CategoryName == 
                    cate)).Include(c => c.SubCategory).ToList();
                    List<SelectListItem> SubCategories= new List<SelectListItem>();
                    //get subcategories from shops
                    for(int i=0;i<c.Count();i++){
                       s.Add(new SelectListItem { Value = c[i].SubCategory.Id, Text = c[i].SubCategory.SubCategoryName  });
                    }
                    ViewBag.SubCategories = SubCategories;
                    return View();
                }
    

    查看:

    @model Shop
        @Html.DropDownListFor(m => m.SubCategoryTypeId, (IEnumerable<SelectListItem>)ViewBag.SubCategories, "select")
    

    【讨论】:

    • 我不明白你的“观点”。你能清除你的“观点”吗?。我想要一个子类别作为下拉菜单找到子类别明智的产品视图,就像我已经的类别明智的产品一样已经完成了。
    • 视图是xxx.cshtml(你要放下拉列表的地方),在ListSubCategories中,你可以按categoryname选择店铺,因为你说你要product-related category,然后你可以从商店,并将它们放到 List 中,使用 viewbag 将列表传递到您要使用下拉列表的页面。
    • 还想要product-related-subcategory使用下拉列表。如果您更新代码,我会很高兴。您可以再次阅读我的帖子。
    • 列表是产品相关分类,_db.Shop.Include(c =&gt; c.Category.Where(x =&gt; x.Category.CategoryName == cate)).Include(c =&gt; c.SubCategory).ToList();,表示子分类来自店铺(产品型号),表示使用分类名称=>获取相关店铺=>从相关店铺获取子分类跨度>
    猜你喜欢
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多