MVC布局VS MVC母版页 [英] MVC Layout VS MVC Master Page

查看:239
本文介绍了MVC布局VS MVC母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习MVC4.我遇到了两种基于Razor模板或母版页创建视图的可能性.

I'm starting learning MVC4. I came across the two possibilities of creating a View based on a Razor template or a Master Page.

我想了解两者之间的实际差异.

I would like to understand the practical differences between the two.

现在,我可以看到,如果我使用母版页创建视图,则可以覆盖多个部分.例如,如果我的主人定义了左列"占位符和主体"占位符,我不仅可以定义特定视图的主体,而且还可以渲染左列"部分中的内容,例如显示控件.绑定到页面所在的上下文(从搜索框到股票报价查看器). 另外,不能通过使用Razor模板来定义母版页,Razor模板比其他语法冗长得多(部分错误: noreferrer>有人设法破解了这一方面.

For now, I can see that if I create a View using a Master Page, I can override several sections. For example, if my Master defines a "left column" placeholder and a "body" placeholder I can not only define the body for a specific View, but I can also render contents in the "left column" section for example to display controls that are bound to the context in which the page is (from a search box to a stock quote viewer). Also, Master Pages cannot be defined by making use of Razor templates, which are much less verbose than other syntax (partially wrong: someone managed to hack this aspect).

使用Razor布局时,我只能在页面中定义一个连续块,这些块可以被特定的View覆盖,并且我应该使用多个布局(不使用DRY)对其他部分进行少量更改页. 我先前的说法正确还是我遗漏了一些东西?

With Razor Layouts, I can only define one contiguous block of the page that can be overridden by specific View, and I should use multiple layouts (breaking DRY) for little changes in other parts of the page. Is my previous statement correct or am I missing something?

很显然,我可以利用jQuery来呈现页面任何部分的内容,但这是另一回事

Obviously I can render contents in any part of the page by making good use of jQuery, but that's another matter

推荐答案

您可以将部分与Razor一起使用.斯科特·古(Scott Gu)在这里发布了有关它们的博客:

You could use sections with Razor. Scott Gu blogged about them here: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

在布局中,您可以根据需要定义任意多个部分:

In your Layout you could define as many sections as you wish:

<div id="leftMenu">
    @RenderSection("LeftMenu", required: false)
</div>

您可以在视图中覆盖的

@section LeftMenu {
    <div>... here comes the left menu for this view ...</div>
}

您还可以测试是否已在视图中定义了一个节,如果没有提供某些默认内容,则可以:

You could also test whether a section has been defined in a view and if not provide some default content:

@if (IsSectionDefined("LeftMenu")) { 
    @RenderSection("LeftMenu")
}
else { 
    <div>Some default left menu</div>
}

这篇关于MVC布局VS MVC母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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