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 ViewsShared_Layout.cshtml.
So I would like to add another shared layout, say, ViewsShared_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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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)
without magic strings.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0