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 6826133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:06:41+00:00 2026-05-26T22:06:41+00:00

How can i close <tr> and open <tr> after 3 loop iterations? I have

  • 0

How can i close <tr> and open <tr> after 3 loop iterations? I have MVC 3 in .NET 4.0. How can I count loop iterations in MVC 3?

Current Code:

@foreach (var articleOnFirstPage in Model.ArticlesOnFirstSite)
{

<tr>
    <td><div class="productsFrame"></div></td>
</tr>

}

I want to get this:

<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
<tr>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
        <td><div class="productsFrame"></div></td>
    </tr>
  • 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-26T22:06:42+00:00Added an answer on May 26, 2026 at 10:06 pm

    You could perform the following pornography in your view:

    @model IEnumerable<Foo>
    <table>
    @foreach (var item in from i in Model.Select((value, index) => new { value, index }) group i.value by i.index / 3 into g select g)
    {
        <tr>
            @foreach (var x in item)
            {
                <td><div class="productsFrame">@x.SomeProperty</div></td>
            }
        </tr>
    }
    </table>
    

    or simply use view models and do the grouping in your controller action which obviously is what I would recommend you. The sole fact that you need to do this means that your view model is not adapted to your view’s requirements which is to group results by 3. So adapt it. Don’t pass IEnumerable<Foo> to your view. Pass IEnumerable<MyViewModel> where obviously MyViewModel will contain the necessary grouping so that in your views you could simply loop or since I hate writing for and foreach loops in views simply use display templates. They will take care of everything and your view will simply look like this:

    <table>
        @HtmlDisplayForModel()
    </table>
    

    Looks better than the initial pornography isn’t it?


    As requested in the comments section here’s how I would implement this using view models.

    As always in an ASP.NET MVC application you start by defining the view models that will reflect the requirements of your view (which I repeat are: show a table with 3 columns):

    public class ItemViewModel
    {
        public string Title { get; set; }
    }
    
    public class MyViewModel
    {
        public IEnumerable<ItemViewModel> Items { get; set; }
    }
    

    then you move on to the controller that will fill and pass this view model to the view:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // Obviously in a real world application the data is your domain model
            // and comes from a repository or a service layer depending on the complexity
            // of your application. I am hardcoding it here for the 
            // purposes of the demonstration
            var data = Enumerable.Range(1, 30).Select(x => new { Title = "title " + x });
    
            var model =
                from i in data.Select((value, index) => new { value, index })
                group i.value by i.index / 3 into g
                select new MyViewModel
                {
                    Items = g.Select(x => new ItemViewModel { Title = x.Title })
                };
    
            return View(model);
        }
    }
    

    and finally you write the corresponding view (~/Views/Home/Index.cshtml):

    @model IEnumerable<MyViewModel>
    <table>
        @Html.DisplayForModel()
    </table>
    

    and the ~/Views/Home/DisplateTemplates/MyViewModel.cshtml display template:

    @model MyViewModel
    <tr>
        @Html.DisplayFor(x => x.Items)
    </tr>
    

    and finally the corresponding ~/Views/Home/DisplateTemplates/ItemViewModel.cshtml display template:

    @model ItemViewModel
    <td>@Html.DisplayFor(x => x.Title)</td>
    

    and that’s pretty much it. Simple, clean, following good practices and conventions.

    Obviously to bring this a step further you would introduce AutoMapper to perform the actual mapping between your domain models and view models and you will end up with a very elegant solution that will look like this:

    public ActionResult Index()
    {
        IEnumerable<DomainModel> data = ...
        var viewModel = Mapper.Map<IEnumerable<DomainModel>, IEnumerable<MyViewModel>>(data);
        return View(viewModel);
    }
    

    or a step further:

    [AutoMap(typeof(IEnumerable<DomainModel>), typeof(IEnumerable<MyViewModel>))]
    public ActionResult Index()
    {
        IEnumerable<DomainModel> data = ...
        return View(data);
    }
    

    Now we are starting to get into serious business.

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

Sidebar

Related Questions

Edit: Updated code after changes I have a for loop that creates markers for
I have a combo box as you can see in the code that takes
I have a foreach loop that starts a process within a try/catch. In the
Does anybody know how I can close all modal dialogs created by Dojo ?
I get errors when viewing forms and there are not any. I can close
How can I close any Office 2007 running application like Word,Excel,Outlook before installing an
Just for fun, how close can we get to debug an application in C#
Can anyone provide me an example of how to use WM_CLOSE to close a
how can I make this jquery script close all previously opened children when entering
How can I know when to close the socket of an HTTP client (I

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.