c# - Mvc get subcategories from category id -
i have 2 sql table categories , subcategories. in subcategories table there categoryid row. how can subcategories categoryid?
public actionresult index() { var model = new blogdb(); model.categories = db.categories.tolist(); model.subcategories = db.subcategories.tolist(); return view(model); }
i want this:
select c.name, s.name categories c left join subcategories s on c.id=s.categoryid
and index view is:
<ul> @foreach (var item in model.categories) { <li> @item.name <ul> @foreach (var sub in model.subcategories) { <li>@sub.name</li> } </ul> </li> } </ul>
if datamodel setup correctly, , assuming category class has icollection of subcategories
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>
Comments
Post a Comment