Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling @Html.Partial to display a partial view belonging to a different controller [duplicate]

I am developing an ASP.NET MVC 3 application, whose content pages have a common pattern of layout elements. However, because the login page does not follow this layout, I cannot place this layout in \Views\Shared\_Layout.cshtml.

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

Is there any way to invoke @Html.Partial for a view belonging to a different controller?

like image 707
pyon Avatar asked May 19 '11 14:05

pyon


People also ask

How do you call a partial view from another controller?

Another best way is to place Partial View inside shared folder & call same partial View from different controller using Shared Folder. And then call it from controller as mentioned above. That's it.

How do I return two partial views from a controller?

You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel.

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.


1 Answers

That's no problem.

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

or

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

Should do the trick.

If you want to pass through the (other) controller, you can use:

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

or any of the other overloads

like image 136
GvS Avatar answered Sep 25 '22 19:09

GvS