Вопрос

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!

Это было полезно?

Решение

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" })

Другие советы

Try this:

 Html.ActionLink("Espanol", "action", "ControllerName", new { Area = "es" }, null)

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top