【发布时间】: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