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

3

10

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!

Limn answered 4/11, 2013 at 23:18 Comment(4)
You've mentioned that you know areas are not the way to go for localization, but I want to stress that they are absolutely the wrong method to use. If you want to implement localization you should have all content that needs to be localized (strings of text that would be displayed to a user for the most part) stored in some external resource such as a database, and then retrieve the content in the appropriate language based on user selection. With your current approach, you will have to maintain an identical copy of your code for every language you support.Lula
Well, it's not just the strings that will change with the language, it's also different layouts and navigation. Thought it might get harder to maintain with some many resource files, and we also can't go back into the application and change it to accept the content in this way. Then I thought it would make more sense to do this via culturally-specific views, actually, but I had a very hard time getting that to work (with different view engines and the like). So, in the time given, I was left with creating an area.Limn
Also, they are only doing this once - the site will only be translated to Spanish.Limn
Maybe it's not relevant to this specific problem, but another option is to define special route maps that deal with the /es/ portion of the URL.Alcaic
K
16

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" })
Kelseykelsi answered 5/11, 2013 at 17:37 Comment(2)
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 { }) codeLimn
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 htmlAttributesMollymollycoddle
M
1

Try this:

 Html.ActionLink("Espanol", "action", "ControllerName", new { Area = "es" }, null)
Morning answered 4/11, 2013 at 23:27 Comment(2)
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.Limn
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?Limn
F
0

If the actionName is only for @Html.ActionLink(), you can simply pass "null" as the actionName. The current actionName will be used.

Finable answered 27/7, 2018 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.