Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4079370
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:47:35+00:00 2026-05-20T17:47:35+00:00

This is very simple, but I’m being rather stupid about it. I have a

  • 0

This is very simple, but I’m being rather stupid about it. I have a list of elements and I need to create a new row after the 4th element. So say I have a list “One, Two, Three, Four, Five, Six” … the resulting HTML table needs to look like this:

+-------------------------+
| One | Two | Three | Four|
+-------------------------+
| Five| Six |       |     |
+-------------------------+

If I were using Javascript it’d be very, very clean, something like this:

for (var i = 0; <= (Items.length - 1); i++) {
if ((i%5) === 0) {
   $("#myTable").append('<tr class="table-rows" />');
   $("#myTable .table-rows:last").append(Items[i]);
}
 else {
   $("#myTable .table-rows:last").append(Items[i]);
}

Well that code might not exactly work, but I’d use the modulo operator to determine when to create a new row then append the data.

You might say that I shouldn’t be using a table for this, and I would agree, but this is not the requirement I’m working under. You might also say that it’d be easier to build the table server side (I’m using ASP.NET MVC, so in a controller), but I have to do this in a view that’s inheriting List as its model.

I really want to break up the items into groups of 4 (again this is ugly code, and I’m omitting the ugly WebForms viewengine markup for clarity):

for (int i = 0; i < Model.Count; i = i + 4) {
<tr>
 <td>Model[i]</td>
 <td>i + 1 < Model.Count ? Model[i + 1] : ""</td>
 <td>i + 2 < Model.Count ? Model[i + 2] : ""</td>
 <td>i + 3 < Model.Count ? Model[i + 3] : ""</td>
</tr>
}

This works for the first set of 4, but then each element gets its own row:

+-------------------------+
| One | Two | Three | Four|
+-------------------------+
| Five|     |       |     |
+-------------------------+
| Six |     |       |     |
+-------------------------+

I feel as if I’m oh so close, what gives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:47:36+00:00Added an answer on May 20, 2026 at 5:47 pm

    There’s no reason you can’t use the Modulo operator in C#, in basically the exact same way you might in Javascript.

    Your C# code as it is also will give you index out of bounds errors unless your data source has exactly divisible-by-four elements.

    EDIT: Example; assuming a Razor C# view:

    @for(int i=0; i<Model.Length;i++){
       if(i%4==0){
          <tr>
       }
       <td>@Model[i].OutPut</td>
       if(i%4==3){
          </tr>
       }
    }
    @if(Model.Length%4!=0){
      </tr>
    }
    

    EDIT: Added the if() construct at the end in re: @48klocs’ comment!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.