Call @ Html.Partial to display a partial view owned by another controller

I am developing an ASP.NET MVC 3 application whose content pages share a common layout element structure. However, since the login page does not match this location, I cannot place this layout in \Views\Shared\_Layout.cshtml .

So, I would like to add another general layout, say \Views\Shared\_Content.cshtml , and call it from the content views ... but, unfortunately, these views belong to different controllers.

Is there a way to call @Html.Partial for a view owned by another controller?

+76
c # asp.net-mvc razor
May 19 '11 at 2:40
source share
2 answers

It's not a problem.

 @Html.Partial("../Controller/View", model) 

or

 @Html.Partial("~/Views/Controller/View.cshtml", model) 

Gotta do the trick.

If you want to go through a (different) controller, you can use:

 @Html.Action("action", "controller", parameters) 

or any other overload

+139
May 19 '11 at 15:24
source share

As GvS said, but I also find it useful to use strongly typed views so that I can write something like

 @Html.Partial(MVC.Student.Index(), model) 

no magic lines.

+6
May 10 '12 at 4:08
source share



All Articles