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

  • Home
  • SEARCH
  • 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 689379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:19:34+00:00 2026-05-14T02:19:34+00:00

I have a master page in my asp.net MVC project, which has code like

  • 0

I have a master page in my asp.net MVC project, which has code like this:

    <div id="menu">
        <ul>
            <li><a href="#" class="current">home</a></li>
            <li><a href="#">add image</a></li>
            <li><a href="#">contact</a></li>
        </ul>
    </div>

Depending on what page I am on, I’d like to move the class="current" attribute to a different <li>.

What is the general pattern that this type of thing is done with on ASP.NET MVC?

  • 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-14T02:19:35+00:00Added an answer on May 14, 2026 at 2:19 am

    Server-side you can use a BaseViewModel that contains information on which link should have the current class. All of your view models would inherit from this base class and the MasterPage would be strongly typed to the BaseViewModel. You can then use the model to set up the links.

    Client-side you can apply a specific id or class that uniquely identifies each link and associates it with a particular url. On page load you can use jQuery (or other) to add the class to the matching link based on the mapping logic for your application.

    I prefer the former as it will work even if javascript isn’t available.

     public class BaseViewModel
     {
          public string CurrentLink { get; set; }
          ...
     }
    

    View logic

     <div id="menu">
        <ul>
            <li><a href="#" class="<%= Model.CurrentLink == "home" ? "current" : "" %>">home</a></li>
            <li><a href="#" class="<%= Model.CurrentLink == "addimage" ? "current" : "" %>">add image</a></li>
            <li><a href="#" class="<%= Model.CurrentLink == "contact" ? "current" : "" %>">contact</a></li>
        </ul>
    </div>
    

    Moreover, I’d probably put together a HTML helper that would generate the menu item for me. Essentially, use a TagBuilder to build up the anchor and apply the class using the model on the ViewContext associated with the HtmlHelper. Wrap this in an li and return it as a string. Makes the page logic much cleaner and, if your links are associated with controllers/actions you can use that information from the ViewContext as well to choose which should be the current link.

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

Sidebar

Related Questions

I have this code in my ASP.NET MVC project master page: <% switch(Request.Browser.Browser) {
I have a hybrid ASP.NET WebForms/MVC project. In my Master Page, I have a
I created a default ASP.net MVC project. In the Master page I have the
I have something like this in Site.Master in an asp.net mvc3 (not razor) project:
I have created an asp.net MVC application.I created a asp:menu in the master page
I have a asp.net mvc site with a master page and it has following
I am working with an ASP.NET MVC application. I have one master page having
I have a master page that contains an ASP.NET server side Menu control (System.Web.UI.WebControls.Menu)
I have an ASP.net master page. In this master, I have all my css
I have an ASP.NET Site that has a single Master Page. On one of

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.