Monday, July 21, 2014

Loop through Model items in ASP.NET MVC

Loop through Model items in ASP.NET MVC

 

MVC3

To loop through Model items in ASP.NET MVC view, use foreach loop in the Controller,which returns Collection of items

 

   1:  @model IEnumerable<BookStore.Models.Category>



Now write foreach loop
 

   1:  <body>
   2:      @if (Model != null)
   3:      {
   4:          foreach (var item in Model)
   5:          {
   6:          <ul id="menu">
   7:   
   8:              <li>
   9:                  @Html.DisplayFor(modelItem => item.BookName)
  10:              </li>
  11:              <li>
  12:                  @Html.DisplayFor(modelItem => item.Title)
  13:                  <br />
  14:              </li>
  15:              <li>
  16:                  @Html.DisplayFor(modelItem => item.BookId)
  17:              </li>
  18:          </ul>
  19:          }
  20:      }
  21:  </body>



 


ThanksSmile


To know more about MVC kindly go through with the links given below:

1 comment :

  1. This comment has been removed by a blog administrator.

    ReplyDelete