Mvc从类别id获取子类别
本文关键字:获取 子类 id Mvc | 更新日期: 2023-09-27 18:02:46
我有2个sql表类别和子类别。在Subcategories表中有一个CategoryID行。我如何获得子类别为categoryid?
public ActionResult Index()
{
var model = new BlogDB();
model.Categories = db.Categories.ToList();
model.Subcategories = db.Subcategories.ToList();
return View(model);
}
我想这样:
select c.Name, s.Name from Categories c left join Subcategories s on c.ID=s.CategoryID
和索引视图是:
<ul>
@foreach (var item in Model.Categories)
{
<li>
@item.Name
<ul>
@foreach (var sub in Model.Subcategories)
{
<li>@sub.Name</li>
}
</ul>
</li>
}
</ul>
如果您的数据模型设置正确,并且假设您的类别类具有子类别的集合
public call Category
{
//.
//.
//.
public virtual List<Subcategory> Subcategories{get;set;}
}
<ul>
@foreach (var item in Model.Categories)
{
<li>
@item.Name
<ul>
@foreach (var sub in item.Subcategories)
{
<li>@sub.Name</li>
}
</ul>
</li>
}
</ul>