Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ActionLink helper that knows what page you're on

Tags:

asp.net-mvc

I have a small MVC site that uses the Html.ActionLink helper for a navbar. One thing I would like to change is that the default ActionLink will render out an html link to a page even if that is the current page.

For example, it creates a link like this:

<a href="/myUrl">Some title...</a>

even if you're already on /myUrl. It would be nice if it would disable that link and maybe insert a special CSS class to show the currently visited page, like this:

<a href="#" class="currentPageCSS">My Url</a>
<a href="/someOtherUrl">Some Other Url</a>

This problem must have been encountered before on loads of MVC sites, so I'm curious to know how other people have tackled it.

like image 917
John Hoge Avatar asked Feb 22 '11 22:02

John Hoge


People also ask

What is an Action Link?

Action Memo, now known as Action Link, turns your handwritten notes into action for immediate use or saves them for a later time.

How does ActionLink work?

ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

How do I pass an object in ActionLink?

If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).

What is Ajax ActionLink?

ActionLink(AjaxHelper, String, String, AjaxOptions) Returns an anchor element that contains the URL to the specified action method; when the action link is clicked, the action method is invoked asynchronously by using JavaScript.


1 Answers

This seems like a good scenario to roll a custom HTML helper. So let's roll it:

public static class LinkExtensions
{
    public static MvcHtmlString MyActionLink(
        this HtmlHelper htmlHelper, 
        string linkText, 
        string action, 
        string controller
    )
    {
        var currentAction = htmlHelper.ViewContext.RouteData.GetRequiredString("action");
        var currentController = htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
        if (action == currentAction && controller == currentController)
        {
            var anchor = new TagBuilder("a");
            anchor.Attributes["href"] = "#";
            anchor.AddCssClass("currentPageCSS");
            anchor.SetInnerText(linkText);
            return MvcHtmlString.Create(anchor.ToString());
        }
        return htmlHelper.ActionLink(linkText, action, controller);
    }
}

and inside your view:

<%= Html.MyActionLink("hello foo", "Index", "Home") %>
<%= Html.MyActionLink("hello bar", "About", "Home") %>
...

and depending on where you are the helper will generate the proper anchor.

like image 117
Darin Dimitrov Avatar answered Nov 10 '22 02:11

Darin Dimitrov