Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 585
EPM Avatar asked Nov 04 '13 23:11

EPM


People also ask

What is htmlactionlink in MVC with example?

The Html.ActionLink creates an anchor element based on parameters supplied. In this example, we will learn how to use Asp .Net MVC Html.ActionLink. We will also create multiple types of Action links like: HtmlActionLink without controller name. HtmlActionLink with controller name. HtmlActionLink with parameters. HtmlActionLink with Area.

How to call action method from another controller in anchor element?

You can see it has automatically added Controller name "Home" in an anchor element. Here "Click Here" is the Link text and "About" is the Action Method you wish to call and CompanyInfo is the Controller name. This will be useful when you wants to call Action Method which resides in other Controller rather than Controller that generated view.

What is the use of action and renderaction in HTML?

This article introduces both @Html.Action and @Html.RenderAction. These are used to call a partial view in another view by action method. As we have other options such as @Html.Partial and @Html.RenderPartial to call a partial view in other views then why do we use @Html.Action and @Html.RenderAction ?

How do I call a controller action with a parameter?

Calling Controller Action with parameter. Calling Controller Action with parameter returning JSON data. The syntax and use of the post method is just like the get method. Here instead of using the get keyword, use the post keyword and all the other things are the same. Calling Controller Action without parameter.


1 Answers

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" })
like image 158
haim770 Avatar answered Sep 20 '22 13:09

haim770