调用@Html. part 以显示属于不同控制器的部分视图

我正在开发一个 ASP.NET MVC3应用程序,它的内容页面有一个共同的布局元素模式。但是,由于登录页面不遵循此布局,因此不能将此布局放在 \Views\Shared\_Layout.cshtml中。

因此,我想添加另一个共享布局,比如说,\Views\Shared\_Content.cshtml,并从内容视图调用它... 但不幸的是,这些视图属于不同的控制器。

是否有办法为属于不同控制器的视图调用 @Html.Partial

225945 次浏览

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

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.