如何使用局部视图列出无限数量的嵌套类别和子类别。(.NET CORE 5.0)
How do I list an indefinite number of nested categories and subcategories with a partial view.(.NET CORE 5.0)
我是编程新手。我想列出嵌套编号不明确的类别。作为我研究的结果,我决定我可以在一个数据库中完成它 table。我认为我应该为此使用递归结构并决定为此使用部分视图。
数据库实体
public class Categories {
public int ID { get; set; }
public int ParentCatID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
}
我正在尝试编写的 PartialView
我可以不使用 PartialView 吗?
我想实现这个输出
class
public class Categories
{
public int ID { get; set; }
public int? ParentCatID { get; set; }
public Categories ParentCat { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public IList<Categories> Children { get; protected set; } = new List<Categories>();
}
视图模型
public class CategoriesViewModel
{
public CategoriesViewModel()
{
ChildItems = new List<CategoriesViewModel>();
}
public long ID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public CategoriesViewModel ParentCatID { get; set; }
public IList<CategoriesViewModel> ChildItems { get; set; }
public void AddChildItem(CategoriesViewModel childItem)
{
childItem.ParentCatID = this;
ChildItems.Add(childItem);
}
}
控制器
public IActionResult List()
{
var categories = _context.ToList(); // example
var categoryItems = new List<CategoriesViewModel>();
var topCategories = categories.Where(x => !x.ParentCatID.HasValue);
foreach (var category in topCategories)
{
var categoryMenuItem = Map(category);
categoryItems.Add(categoryMenuItem);
}
return View(categoryItems);
}
private CategoriesViewModel Map(Categories category)
{
var categoryMenuItem = new CategoriesViewModel
{
ID = category.ID,
CategoryName = category.CategoryName,
Instruction = category.Instruction
};
var childCategories = category.Children;
foreach (var childCategory in childCategories)
{
var childCategoryMenuItem = Map(childCategory);
categoryMenuItem.AddChildItem(childCategoryMenuItem);
}
return categoryMenuItem;
}
查看
@model IEnumerable<CategoriesViewModel>
@foreach (var item in Model)
{
if (item.ChildItems.Any())
{
<li>
<span>@item.CategoryName</span>
<ul class="dropdown">
<li>
@foreach (var childItem in item.ChildItems)
{
<span>@childItem.CategoryName</span>
}
</li>
</ul>
</li>
}
else
{
<li><span>@item.CategoryName</span></li>
}
}
我是编程新手。我想列出嵌套编号不明确的类别。作为我研究的结果,我决定我可以在一个数据库中完成它 table。我认为我应该为此使用递归结构并决定为此使用部分视图。
数据库实体
public class Categories {
public int ID { get; set; }
public int ParentCatID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
}
我正在尝试编写的 PartialView
我可以不使用 PartialView 吗?
我想实现这个输出
class
public class Categories
{
public int ID { get; set; }
public int? ParentCatID { get; set; }
public Categories ParentCat { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public IList<Categories> Children { get; protected set; } = new List<Categories>();
}
视图模型
public class CategoriesViewModel
{
public CategoriesViewModel()
{
ChildItems = new List<CategoriesViewModel>();
}
public long ID { get; set; }
public string CategoryName { get; set; }
public string Instruction { get; set; }
public CategoriesViewModel ParentCatID { get; set; }
public IList<CategoriesViewModel> ChildItems { get; set; }
public void AddChildItem(CategoriesViewModel childItem)
{
childItem.ParentCatID = this;
ChildItems.Add(childItem);
}
}
控制器
public IActionResult List()
{
var categories = _context.ToList(); // example
var categoryItems = new List<CategoriesViewModel>();
var topCategories = categories.Where(x => !x.ParentCatID.HasValue);
foreach (var category in topCategories)
{
var categoryMenuItem = Map(category);
categoryItems.Add(categoryMenuItem);
}
return View(categoryItems);
}
private CategoriesViewModel Map(Categories category)
{
var categoryMenuItem = new CategoriesViewModel
{
ID = category.ID,
CategoryName = category.CategoryName,
Instruction = category.Instruction
};
var childCategories = category.Children;
foreach (var childCategory in childCategories)
{
var childCategoryMenuItem = Map(childCategory);
categoryMenuItem.AddChildItem(childCategoryMenuItem);
}
return categoryMenuItem;
}
查看
@model IEnumerable<CategoriesViewModel>
@foreach (var item in Model)
{
if (item.ChildItems.Any())
{
<li>
<span>@item.CategoryName</span>
<ul class="dropdown">
<li>
@foreach (var childItem in item.ChildItems)
{
<span>@childItem.CategoryName</span>
}
</li>
</ul>
</li>
}
else
{
<li><span>@item.CategoryName</span></li>
}
}