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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -