Display records in table with fixed number of columns


<table> @{var counter = 1; } <tr> @foreach (var item in Model) { <td> @Html.DisplayFor(modelItem => item.courseName) @Html.DisplayFor(modelItem => item.courseSubject) @Html.DisplayFor(modelItem => item.institute) </td> if (counter % 3 == 0) { @:</tr><tr> } } </tr> </table>

I want to display records in a table with 3 columns per row. This is the way how I tried. But still all records in one row. How i can made it.


You need to update the counter variable inside the loop. That should do it.

<table> @{var counter = 1; } <tr> @foreach (var item in Model) { <td> @Html.DisplayFor(modelItem => item.courseName) </td> if(counter%3 == 0) { @:</tr><tr> } counter++; } </tr> </table>


If I am reading your question correctly, you want 3 columns per row?

If that's the case make the following change to your HTML structure. You need to add three <td> elements to make three columns

@foreach (var item in Model) { <td> @Html.DisplayFor(modelItem => item.courseName) </td> <td> @Html.DisplayFor(modelItem => item.courseSubject) </td> <td> @Html.DisplayFor(modelItem => item.institute) </td> if (counter % 3 == 0) { @:</tr><tr> } }

Like Shyju suggested in his answer, I'm not really sure what you're doing with a counter there, as it's not being incremented on each iteration of the loop.


  • Save data to csv file using Python
  • Display records in table with fixed number of columns
  • Xml query resulting in error in sql server
  • View overlap after updating fragment with Asynctask
  • How can I set table column widths in an MVC view?
  • ASP.NET Display Templates - No output
  • Knockout observableArray not updating JQuery Accordion
  • vb.net bindingSource and bindingNavigation programatically
  • Element not being displayed when div is removed in react
  • Attaching simple metadata to a MySQL database
  • Database field containing HTML display raw text
  • How to use html helper for a generic collection?
  • MySQL: why 'explain' command yields different results on same SQL statement?
  • Is it possible to display more than one item in a cell with a foreach condition?
  • HTML XPath: Selectively avoiding tags when extracting text
  • Get selected data
  • MDX: How to Retrieve data Based on Latest Date For Each Student Id
  • form tag disappear in foreach loop razor MVC 3
  • SQL Procedures - Comparing values
  • SQL: Getting the physical size of a subset of a table
  • How to open html table in xls on click of a button
  • how to get username into sql trigger when multiple users signed on from asp membership
  • How do I remove all but some records based on a threshold?
  • How do I get the list of bad records that didn't load in Bigquery?
  • Sequential (transactional) API calls in angular 4 with state management
  • Custom validator control occupying space even though display set to dynamic
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • Traverse Array and Display in markup
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • How to set the response of a form post action to a iframe source?
  • JaxB to read class hierarchy