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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:04:12+00:00 2026-05-23T02:04:12+00:00

I am trying to display a list of items that should toggle a class

  • 0

I am trying to display a list of items that should toggle a class for styling purposes. The idea is to create a foreach loop that will cycle through all the myObj in the Model.

I tried the following code which does not work (because I’m doing it wrong)

@{ int i = 2;
   foreach(var myObj in Model)
   {
        if (i % 2 == 0)
        {
            <div class="class1">
        }
        else
        {
            <div class="class2">
        }
        Html.Partial(...);
        </div>
         i += 1;
   }     
}

What is the proper way to accomplish this?

Update

I also tried the following code that, although compiles, does not render any HTML code within (and I’m sure there are objects in Model).

@{ int i = 2;
   foreach(var myObj in Model)
   {
        if (i % 2 == 0)
        {
            @:<div class="class1">
        }
        else
        {
            @:<div class="class2">
        }
        Html.Partial(...);
        @:</div>

        i += 1;
   }

}

This is the partial class being called

<div class="class">
    <div class="class2">
        @if (string.IsNullOrEmpty(var))
        {
            @var2
        }
        else
        {
            @var
        }
    </div>
    <div class="class3">
        @var3
    </div>
</div>
<div class="class4">
    <p>var4</p>
    <ul class="class5">
        <li>element1</li>
        <li>element2</li>
    </ul>
</div>

I’m sorry I can’t post the actual names and variables.

  • 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-23T02:04:13+00:00Added an answer on May 23, 2026 at 2:04 am

    Let’s start with improving your code.

    • Improvement step 1:

      @foreach(var myObj in Model.Select((model, index) => new { model, index }))
      {
          <div class="class@(myObj.index % 2 == 0 ? "1" : "2")">
              @Html.Partial("_Foo", myObj.model)
          </div>
      }
      
    • Improvement Step 2 (using a custom HTML helper for the class):

      @foreach(var myObj in Model.Select((model, index) => new { model, index }))
      {
          <div class="@Html.MyClass(myObj.index)">
              @Html.Partial("_Foo", myObj.model)
          </div>
      }
      

      where MyClass is defined like this:

      public static string MyClass(this HtmlHelper html, int index)
      {
          return (index % 2 == 0) ? "class1" : "class2";
      }
      
    • Improvement step 3 which is the state of the art (using Templated Razor Delegates):

      @Model.List(
          @<div class="@item.MyClass">
              @Html.Partial("_Foo", @item.Model)
          </div>
      )
      

      where the List extension method looks like this:

      public class ModelHolder<T>
      {
          public T Model { get; set; }
          public string MyClass { get; set; }
      }
      
      public static class RazorExtensions
      {
          public static HelperResult List<T>(
              this IEnumerable<T> items,
              Func<ModelHolder<T>, HelperResult> template
          )
          {
              return new HelperResult(writer =>
              {
                  foreach (var item in items.Select((model, index) => new { model, index }))
                  {
                      var myClass = item.index % 2 == 0 ? "class1" : "class2";
                      template(new ModelHolder<T> { Model = item.model, MyClass = myClass }).WriteTo(writer);
                  }
              });
          }
      }
      

    I vote for improvement number 3 which is far better and more concise than the original foreach loop.

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

Sidebar

Related Questions

I am trying to create a simple app that displays a list of items
im trying to write an app that will display a list off lines from
I am currently trying to build a cakephp app that will list items from
I'm trying to display a list of items that do not already exist in
I am trying to display a list of items in a datagrid from an
I am just trying to display a list from an array that I have
I am trying to create a page to display a list of links for
I'm trying to set up a page where I display a list of items
I have some list items with anchor tags, that are set to display block
I am trying to loop through the list items of several ULs of a

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.