asp.net mvc - iterate two different foreach according to equality in c# -


what trying achieve is, separately output posts ordered category. if theres no posts in category can null. give me hint or other approach?

@foreach (var category in model.categories) {  <div><a href="javascript:void(0)">@category.name</a></div> } 

here need list posts according category.

@foreach (var post in model.posts) {    <div>                   <div>         <a href="@url.action("index", "post", new { id = post.id })" target="_self">             <img src="@post.image" alt="@post.title" width="93" height="69" />                   </a>      </div>      <h6 class="mkd-pt-seven-title">          <a itemprop="url" class="mkd-pt-link" href="@url.action("index", "post", new { id = post.id })" target="_self">@html.raw(post.title)...</a>      </h6>    </div> } 

here model im getting categories , posts not querying getallpostsbycategory if how specify in view anyway. have 2 different foreachs.

public actionresult main()     {         var model = new mainviewmodel         {             posts = _postservice.getallpostswithcategories(new getpostsinput { }),                            categories = _categoryservice.getallcategories(new getcategoriesinput { })         };         return view(model);     } 

// there many many relation.

public class post {     public string title { get; set; }      public icollection<category> categories { get; set; }  }  public class category  {     public string name { get; set; }      public virtual icollection<post> post { get; set; } } 

here picture want end with:

enter image description here


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -