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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:35:46+00:00 2026-05-12T22:35:46+00:00

I am writing a helper for my application which writes out a menu item

  • 0

I am writing a helper for my application which writes out a menu item for a given strongly typed controller/action as follows:

<%= Html.MenuLink<WhateverController>(c => c.WhateverAction(), "Whatever") %>

As part of this process, I would like to apply the class of active to the outputted link if the current page and the page linked to are the same. I figure the best way of doing it is to compare the contents of the RouteValueDictionary for the current request to that of the result of the Expression given to the helper method. However, I cannot figure out a good way of comparing whether the items in the two RouteValueDictionarys are the same.

Is there a simple way of doing this? I effectively want to complete it in the following way:

public static string MenuLink<T>(this HtmlHelper html, Expression<Action<T>> action, string linkText) where T : Controller
{
    // var link = html.ActionLink<T>(action, linkText, new {}); // Not important yet

    var routeValues = Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression<T>(action);  // Might change?
    var currentRouteVals = html.ViewContext.RouteData.Values;
    bool isActivePage = /* are the contents of routeValues also 
                           inside currentRouteValues? */

    var tb = new TagBuilder("li");
    // Continues...
}

I have tried using the built-in comparison (==), but it seems that it is using the default equality implementation and therefore returns false since they are not the same instance. I have also tried the following:

bool isActivePage = routeValues.All(x => currentRouteVals.ContainsValue(x));

but that doesn’t work either. Am I completely barking up the wrong tree?

  • 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-12T22:35:47+00:00Added an answer on May 12, 2026 at 10:35 pm

    Bah, I post the question and then figure out the answer. Am posting here in case anyone else faces this problem. The line I needed was as follows:

    bool isActivePage = routeValues.All(x => x.Value.ToString() == (currentRouteVals[x.Key].ToString()));
    

    Turns out it was originally comparing them as objects, not as strings. Converting them to strings compares them as one would hope, and now all is well.

    It should be noted that they should be compared around this way. If they are compared round the other way, the RouteValueDictionary may contain things other than those you care about (such as an “id” value). Or at least that’s how I understand it at the moment. Further experimentation may require me to revisit this…

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

Sidebar

Ask A Question

Stats

  • Questions 251k
  • Answers 251k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Simple solution: <Button Tag="{Binding Code}" ...> In your handler, cast… May 13, 2026 at 9:25 am
  • Editorial Team
    Editorial Team added an answer I would use some form of Inversion Of Control. To… May 13, 2026 at 9:25 am
  • Editorial Team
    Editorial Team added an answer There's probably a set-based solution for this, in which case… May 13, 2026 at 9:25 am

Related Questions

First of all, the keylogger that i am developing is not at all for
I have been evolving an automation and statistics generation program through a series of
I am working on a VB6 application that uses an ADODB.Recordset object to dump
I am trying to explain to a non-developer the difference between an API an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.