如果PartialView使用指定的布局? [英] Should the PartialView use the specified Layout?

查看:160
本文介绍了如果PartialView使用指定的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个操作使用中调用PartialView()的布局?

Should calling PartialView() within an action use the layout?

下面是我的操作:

public ActionResult SomeAction()
{
    if (Request.IsAjaxRequest())
        return PartialView();
    else
        return View();

}

下面是我的_ViewStart.cshtml:

Here's my _ViewStart.cshtml:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

情景1 :行动的呼吁没有指定布局的视图。

Scenario #1: Action calls a view with no layout specified.

@{
    //Layout = "~/Views/Shared/_SomeLayout.cshtml";
}
<h2>View!</h2>


  • 查看的结果:查看包装在布局

  • PartialView的结果:视图不裹布局

  • 这是由从达林季米特洛夫这种反应。

    情景2 :行动电话用指定的布局视图。

    Scenario #2: Action calls a view with a layout specified.

    @{
        Layout = "~/Views/Shared/_SomeLayout.cshtml";
    }
    <h2>View!</h2>
    


    • 查看的结果:查看包装在布局

    • PartialView的结果:视图的还是裹在布局

      • View's Result: The view is wrapped in a layout
      • PartialView's Result: The view is still wrapped in a layout
      • 这也似乎是由从达林季米特洛夫这个其他响应。 (注意:尽管他的答案是一个包罗了所有的AJAX请求,这是一个问题,其中OP有两个观点,一个完整的,一个部分反应)

        This also seems to be backed up by this other response from Darin Dimitrov. (NOTE: Even though his answer is a catch-all for AJAX requests, this was the response to a question where the OP had two views, one full and one partial.)

        于是在第一,达林则解释说,如果你不想要的布局,使用PartialView(),但在第二个他说如果你不想要的布局,那么这里有一个解决方法。

        So on the first, Darin is explaining that if you don't want a layout, use PartialView(), but in the second one he is saying if you don't want a layout, then here's a workaround.

        如果有什么我失踪或者为什么是这样有人能向我解释。无论什么样的达林说,如果我只设置在布局 _ViewStart.cshtml ,那么我可以用PartialView()忽略它,但如果我在视图设置另一个布局本身,那我也不能忽视它。

        Can someone explain to me if there is something I'm missing or why it is this way. Regardless of what Darin has said, if I only set the layout in _ViewStart.cshtml, then I can ignore it with PartialView(), but if I set another layout in the View itself, then I can't ignore it.

        这是否有道理?我应该能够忽略这两个布局?如果不是,为什么?

        Does this make sense? Should I be able to ignore both layouts? If not, why?

        推荐答案

        整页或部分页面的渲染过程是相同的剃刀,因为他们使用的是相同的基类。对于渲染处理既充分页面和部分页面创建一个RazorView对象,但有不同的构造函数的参数。
        从源头code这是两个方法来渲染视图。
        System.Web.Mvc命名空间 RazorViewEngine.cs

        Rendering process of full page or partial page is same in Razor, because they are using same base classes. For rendering process both full page and partial page create a RazorView object but with different constructor parameters. From source code these are the two methods for rendering views. System.Web.Mvc namespace RazorViewEngine.cs

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
            {
                return new RazorView(controllerContext, partialPath,
                                         layoutPath: null, runViewStartPages: false, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator)
                {
                        DisplayModeProvider = DisplayModeProvider
                };
            }
        
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
            {
                var view = new RazorView(controllerContext, viewPath,
                                             layoutPath: masterPath, runViewStartPages: true, viewStartFileExtensions: FileExtensions, viewPageActivator: ViewPageActivator)
                {
                        DisplayModeProvider = DisplayModeProvider
                };
                return view;
            }
        

        请参阅创建局部视图时, layoutPath:空,runViewStartPages:在构造函数中的参数虚假。在您的场景#1,这是什么原因查看包裹在布局,但不partialView。根据这些方法返回PartialView时,ViewStart不执行,但在返回查看时,它的作用。

        See that when creating partial view, layoutPath:null,runViewStartPages:false in constructor parameters. In your scenario #1, this is why View wrapped in a layout but partialView not. According to these methods when returning PartialView, ViewStart does not executed but when returning View it does.

        喜欢在构造函数的参数指定partialView默认布局为空。但是ViewActionResult返回之后,可能已被指定重写布局。这是在你的场景#2会发生什么。通过声明布局=〜/查看/共享/ _SomeLayout.cshtml; 您指定 WebPageBase.Layout 属性一些道路,这将覆盖 layoutPath:空。即使你返回PartialView()剃刀将产生完整视图。如果您声明布局= NULL同样的方式; 在你看来,即使你从返回控制器的Razor视图()将产生partialView。因此,无论你的看法会不会,如果你返回确定部分或完整的视图查看() PartialView()。由布局属性是否为空或一些path.So为更具可读性code,当你想生成partialView使用返回PartialView()并没有定义确定您的视图中的任何布局。

        Default layout for partialView is null like specified in the constructor parameter. But after ViewActionResult returns, overriden layout might have been specified. That is what happens in your scenario #2. By declaringLayout = "~/Views/Shared/_SomeLayout.cshtml"; you are specifying WebPageBase.Layout property to some path and this overrides layoutPath:null. Even if you return PartialView() razor will generate complete view. In the same way if you declare Layout = null; in your view and even if you return View() from controller Razor will generate partialView. So whether your view will be partial or complete view not determined by if you return View() or PartialView(). That is determined by whether Layout property is null or some path.So for more readable code, when you want to generate partialView use return PartialView() and do not define any Layout inside your view.

        这篇关于如果PartialView使用指定的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆