Get current action and controller and use it as a variable in an Html.ActionLink?

11,113

Solution 1

Use ViewContext.RouteData to determine current Controller and Action:

@Html.ActionLink("Spanish Version", 
                 ViewContext.RouteData.Values["action"] as String,
                 ViewContext.RouteData.Values["controller"] as String,
                 new { Area = "es" })

Solution 2

Try this:

 Html.ActionLink("Espanol", "action", "ControllerName", new { Area = "es" }, null)
Share:
11,113
EPM
Author by

EPM

Web Developer

Updated on June 19, 2022

Comments

  • EPM
    EPM almost 2 years

    I need to be able to dynamically retrieve the current action and controller name of whatever page you're on, and actually use them to create a new HTML.ActionLink that links to the same action and controller name, but in a different area. So I guess I need to retrieve the current action and controller name as variables to use in building a new HTML.ActionLink.

    So, if I'm on the www.site.com/about page, I need to have a link dynamically generated to the www.site.com/es/about page.

    I've basically built a spanish translated version of my site in a separate area folder (with the same named controllers and actions and views, just content is all in spanish). I need the user to be able to toggle back and forth between the english version of the page (which is the default, and resides in the root site views) and the spanish version (whose views resides in the area folder "es") of whichever page they're currently on. I can't "hardcode" these links because I need this in a shared partial view which is the _topNavigation in my _Layout used on every page.

    Please let me know if I need to clarify. I'm sure using "areas" wasn't really the way to go when localizing an application, but I'm still trying hard to teach myself asp.net MVC. I read many MANY tutorials and examples on localization, and I could just not get them to work or make sense.

    I should also add that I already know how to use HTML.ActionLink to go back and forth between the areas. I've managed to create the correct HTML.ActionLinks to any of the views in the spanish (es) area, and to any of the views in the default site. So that is not my question.

    Any help is greatly appreciated! Thanks!

  • EPM
    EPM over 10 years
    Thank you for your help. Unfortunately, it's a bit more involved. I actually need to programatically get the current controller name and action of the current page that someone is on (for any page) - and, from that, create a new link to a controller with the same name and action - only in the "es" area. Ex: someone lands on the "where-to-buy" view from the "About" controller in the root, the system grabs this information and a new link is created to the where-to-buy view from the "About" controller, but in the "es" area. Not sure how else to explain? I hope that makes sense.
  • EPM
    EPM over 10 years
    or, pehaps, can someone tell me of a way to use variables for the action and controller name in an HTML.ActionLink? Is that possible? I know you append variables at the end, but could you use variables for the action and controller name?
  • EPM
    EPM over 10 years
    That worked beautifully! Thank you so so so much!!!! The only thing I had to add was: code, new { }code at the end. So it became: code @Html.ActionLink("Spanish Version", ViewContext.RouteData.Values["action"] as String, ViewContext.RouteData.Values["controller"] as String, new { Area = "es" }, new { }) code
  • Admin
    Admin about 6 years
    That is not necessary at all. You can just use @Html.ActionLink("Spanish Version", null, null, new { Area = "es" }, null) - and note your code does add the area as a route value - you need to use the correct overload and add a value for the htmlAttributes