简体   繁体   English

动态类别明智的子类别的下拉列表

[英]Dynamically Category wise subcategory's dropdown list

I want to create a dynamical separate product-related category wise sub category's dropdown list and show product separately.我想创建一个动态的单独的产品相关类别明智的子类别下拉列表并单独显示产品。 already I created a dynamically category-wise product.我已经创建了一个动态类别的产品。 but I don't understand how I will create dynamically subcategory which is category wise.但我不明白我将如何动态创建类别明智的子类别。 already I made a relationship between category, subcategory, and product model.我已经建立了类别、子类别和产品模型之间的关系。

Here is my code:这是我的代码:

Model模型

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

Component成分

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

Default.cshtml默认.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 and its view 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>



Output :输出 :

在此处输入图片说明

I already use @(await Component.InvokeAsync("CategoryWiseMenu")) in Layout page.in the above output, I successfully created a dynamically category wise product.我已经在布局页面中使用了@(await Component.InvokeAsync("CategoryWiseMenu"))在上面的输出中,我成功地创建了一个动态类别明智的产品。 but I want dynamical separate product-related category wise sub category's dropdown list.但我想要动态单独的与产品相关的类别明智的子类别的下拉列表。 What's the solution.有什么解决办法。

Get shops from db with categoryName,and then get subcategories from the shops.从带有 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();
            }

View:看法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM