Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net MVC - Sharing partials between areas

is there some way to share a partial razor view between areas?

For example a login partial, it is found if i use @Html.Partial("_LoginPartial") but the URLs Html.ActionLink generates are local to the calling area (even though the partial itself is not part of the area).

_LoginPartial.cshtml is in /Views/Shared/_LoginPartial.cshtml
Calling view is inside /Areas/Somearea/Views

Links generated are like: http://example.com/Somearea/Account/Login
But should always be: http://example.com/Account/Login

Partial view source:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink(@Messages.Logout, "Logout", "Account") ]</text>
}
else {
    @:[ @Html.ActionLink(@Messages.Login, "Login", "Account") ]
}

Thanks

like image 759
Fionn Avatar asked Mar 05 '11 00:03

Fionn


People also ask

How do you call a partial view in another view in MVC?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

Can we use multiple partial view in MVC?

Thank you for answer but there is no multiple partial.

Can we have layout for partial view?

Partial views shouldn't be used to maintain common layout elements. Common layout elements should be specified in _Layout. cshtml files. Don't use a partial view where complex rendering logic or code execution is required to render the markup.


1 Answers

You can specify the area (or lack of one) in the ActionLink() method:

Html.ActionLink(@Messages.Logout, "Logout", "Account", new { Area = "" }, new{})

This will ensure the link does not resolve to a URL within the current area.

like image 107
Jonathan Freeland Avatar answered Oct 05 '22 22:10

Jonathan Freeland