How to specify an area name in an action link?

Viewed 88090

I have a shared master page which I am using from 2 different areas in my mvc 2 app. The master page has an action link which currently specifies the controller and action, but of course the link doesn't work if I'm in the wrong area. I see no overload for actionlink that takes an area parameter, is it possible to do?

9 Answers

Figured it out..

Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})

In my ASP Net Core app, I simply add the area to the html attributes like so:

@Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" , id = @Model.ID, name = @Model.name })

If you can not use standart web aplication link like About, Home, Contac etc from area. You change lines

@Html.ActionLink("Ana Sayfa", "Index", "Home", new { area = "" }, new {})

from

Views\Shared_Layout.cshtml

Using

@Html.ActionLink("DisplayText", "ActionName", "ControllerName", new { area = "AreaName"}, null)

Will achieve what you are after.

The DisplayText is what will display (The same as <a href="#">DisplayText</a>), the ActionName is the method being called in the controller and ControllerName is obviously the controller you want to call! The next parameter is 'Route Value' where you would add your area. If you are currently in an Area and want to navigate back to your root Home/Index for example, you would leave the value as an empty string, eg new { area = ""}. The final value is 'Html Attributes' and where you would add a class if you wished and should be a null if dont have any attributes to add. But as the last parameter is seen as 'Html Attributes'; in order for Route Values to be recognised, this should be null

Related