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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:07:10+00:00 2026-05-12T17:07:10+00:00

I render a list of items like: <ul> <li>Something 1</li> <li>Something 2</li> <li>Something 3</li>

  • 0

I render a list of items like:

<ul>
  <li>Something 1</li>
  <li>Something 2</li>
  <li>Something 3</li>
  <li>Something 4</li>
  <li>Something 5</li>
</ul>

Depending on the route I want to add a class to one of the <li> tags so that I can highlight.

Maybe something like:

<ul>
<% if (this.Context.Request.RawUrl.Contains(something1Var)) { %>
<li class="highlight">
<% } else { <%>
<li>
<% } %>
Something 1</li>

<% if (this.Context.Request.RawUrl.Contains(something2Var)) { %>
<li class="highlight">
<% } else { <%>
<li>
<% } %>
Something 2</li>

<% if (this.Context.Request.RawUrl.Contains(something3Var)) { %>
<li class="highlight">
<% } else { <%>
<li>
<% } %>
Something 3</li>
<% if (this.Context.Request.RawUrl.Contains(something3Var)) { %>
<li class="highlight">
<% } else { <%>
<li>
<% } %>
Something 4</li>

<% if (this.Context.Request.RawUrl.Contains(something5Var)) { %>
<li class="highlight">
<% } else { <%>
<li>
<% } %>
Something 5</li>
</ul>

It does work but it seems a bit verbose. Can anyone suggest a better method of doing this sort of thing?

  • 1 1 Answer
  • 1 View
  • 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-12T17:07:10+00:00Added an answer on May 12, 2026 at 5:07 pm

    I’d argue that this kind of logic is better isolated in the controller. Consider making a lightweight class to represent your menu items, like this:

    public class MenuLinkItem {
        public string Text { get; set; }
        public bool Highlight { get; set; }
    }
    

    Then in your controller method, build a list of these, set the CSS class on the highlighted element, and pass the result to the view via the ViewData:

    public class MyController : Controller { 
      private MenuLinkItem MakeMenuLinkItem(string text, string urlFragment) {
         return (new MenuLinkItem() {
            Text = text,
            Highlight = (this.Context.Request.RawUrl.Contains(urlFragment))
         }
      }   
    
      private IList<MenuLinkItem> GetMenuLinkItems() {
          var items = new List<MenuLinkItem>();  
    
         items.Add(MakeMenuLinkItem("Something 1", something1var));
         items.Add(MakeMenuLinkItem("Something 2", something2var));
         items.Add(MakeMenuLinkItem("Something 3", something3var));
         items.Add(MakeMenuLinkItem("Something 4", something4var));
         items.Add(MakeMenuLinkItem("Something 5", something5var));
         return(items);
      }
    
      public ActionResult Index() {
          ViewData["menuLinkItems"] = GetMenuLinkItems();
          /* other controller logic here */
          return(View());
      }
    }
    

    Then, in your actual view code, you render your menu like this:

    <ul>
    <% foreach(var item in (IList<LinkMenuItem>)ViewData["menuLinkItems"])) { %>
      <li <%= item.Highlight ? "class=\"highlight\"" : ""%>><%=item.Text%></li>
    <% } %>
    </ul>
    

    The controller’s making the decisions about which links are highlighted, and the view is converting those decisions into an HTML/CSS representation that makes sense to your users.

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

Sidebar

Related Questions

I want to render a profile user container that contains a list of labels
I'm trying to create a simple markup with templates. Something like: <div class=list data-bind=template:
I want to add items in a LaTeX-document. Say for example, that I want
I have something like this: <ul> <li>User</li> <li>Admin</li> .. </ul> and want to render
I would like to render a list of HTML links in ASP.NET MVC. Note
I'm using dust.js to render a list of variable names that are sometimes long
I would like to re-render a list made using Marionette.ItemView when the corresponding model
I am creating an epub reader. In that I want to list out .epub
With ERB you can fragment cache individual records in a list view like so:
I'm using Backbone.js to render a list of items. THis is working, I have

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.