How to specify an area name in an action link?
Asked Answered
A

9

158

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?

Attitudinize answered 10/1, 2010 at 6:10 Comment(0)
A
250

Figured it out..

Html.ActionLink("Link Text", "ActionName", "ControllerName", new { Area = "AreaName" }, new{})
Attitudinize answered 10/1, 2010 at 6:20 Comment(4)
@Pure.Krome - yes. Nothing intuitive about it!Attitudinize
Make sure there is not white space before or after the Areaname. Otherwise it will just ignore it... and then will make you wonder four hours what is wrong.. just like me.Hypnos
How do you add parameters in there?Collegiate
How does this look like translated into link? I mean - when form is rendered and returned to the client.Viridissa
G
79

Something I ran into right after this, that I imagine others might run into: If you need to link from within an area to an action not in an area, you still need to specify the Area as empty string.

For instance, I moved some MVC code into an area, and found I needed to update urls in the master page that referenced other pages on the site.

To specify an url to something not in an area, use

Html.ActionLink("home", "Index", new { area = "", controller = "Home" })
Gustin answered 25/4, 2010 at 20:52 Comment(1)
This is a very good tip! But it gives not expected results with MVC 2.. Small correction - Html.ActionLink("home", "Index", new { area = "", controller = "Home" })Quiteri
N
16

Use:

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

Note:4th parameter is to pass route Values, if you pass an empty parameter it will consider root structure and if you pass appropriate value it use it as area.

Also do not forget to use null or new{} as the 5th parameter because passing null or new {} while creating action link will not overload method for (text,action,controller,route data) or its (text,action,controller,route data,html attribute) so use the proper method

Ninefold answered 27/3, 2014 at 8:0 Comment(0)
B
11

In MVC2 giving area="root" worked for me as below

Html.ActionLink("Home", "Index", "Home", new { Area = "root" }, new{})
Brittani answered 14/10, 2011 at 7:44 Comment(0)
A
6

A neat trick you can do if you are using an area a lot in a View is define it as a variable at the top:

@{ var awesomeArea = new { area = "Awesome" }; }

@Html.Action("Something", "Somewhere", awesomeArea)
@Html.ActionLink("Stuff", "FooBar", awesomeArea)
Annmarie answered 31/10, 2014 at 15:32 Comment(0)
S
4

Here is what I came up with as a solution to allow a user to link to the pre-built authentication systems.

Each of my areas has a version of the _LoginPartial.cshtml file.

I probably could get the application to use a single version of the file, however I kept running into errors when trying to use a single login partial.

It is only a slight modification to the original generated loginpartial, but it seems to work well when used in specific areas.

Here is the code that gets used in all of them:

@if (Request.IsAuthenticated)
{
    <text>
    Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", new { area = "" }, htmlAttributes: new { @class = "username", title = "Manage" })!
    @using (Html.BeginForm("LogOff", "Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm" }))
 {
        @Html.AntiForgeryToken()
        <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
 }
    </text>
}
else
{
    <ul>
        <li>@Html.ActionLink("Register", "Register", "Account", new { area = "" }, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", new { area = "" }, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}
Settlement answered 22/9, 2012 at 3:53 Comment(1)
Explicitly setting the htmlAttributes: worked very well for using multiple parameters.Nabila
D
2

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

Dissuasion answered 4/5, 2018 at 20:26 Comment(0)
P
2

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 })
Paola answered 12/6, 2019 at 18:6 Comment(0)
M
1

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

Mitigate answered 12/3, 2020 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.