原文地址:http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in

RenderBody, RenderPage and RenderSection methods in MVC 3

In this article we will learn about the three methods of MVC 3 and those are RenderBodyRenderPage, andRenderSection. We will learn by the following topics:

  • RenderBody

    • What is RenderBody?
    • How RenderBodyworks?
    • RenderBody Example
  • RenderPage
    • What is RenderPage?
    • How RenderPageworks?
    • RenderPage example
  • RenderSection
    • What is RenderPage?
    • How RenderPageworks?
    • RenderPage Example

Now go to in detail…

RenderBody

What is RenderBody?

In layout pages, renders the portion of a content page that is not within a named section. [MSDN]

How RenderBody works (graphical presentation)?

RenderBody Example

It’s simple. Just create a ASP.NET MVC 3 web application by visual studio 2010. After creating this application, you will see that some files and folders are created by default. After that open the _layout.cshtml file from views/Shared folder.  Basically this file will be used as a standard layout for all the page in project. Keep in mind that you can create more then one layout page in a application and to use layout page in other page is optional. The _layout.cshtml file consist the following code.

 Collapse | Copy Code
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<div id="menucontainer">
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
</div>
<div id="main">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
</body>
</html>

Now open another file called index.cshtml from views/home. This file consists of the following code:

 Collapse | Copy Code
@{
ViewBag.Title = "Home Page";
} <h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

Main thing is that by the above code you couldn’t find which layout page is being used by this index page. But there is little tricks done at MVC3. You will get a file called _ViewStart.cshtml at views folder. This file consist of  following code.

 Collapse | Copy Code
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

This code means that by default all the content pages will follow the _Layout.cshtml layout page.  Now if we consolidate the _layout.cshtml and index.cshtml page both, we will get the following code.

 Collapse | Copy Code
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My MVC Application</h1>
</div>
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<div id="menucontainer">
<ul id="menu">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
</ul>
</div>
</div>
<div id="main"><h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</div>
<div id="footer">
</div>
</div>
</body>
</html>

It’s nothing complicated, it’s just replacing the code of RenderBody() of layout page by the code of content page.

if you want to use different layout for different content pages, then create a layout page as like _Layout.cshtml and just copy below code to your desired content page.

 Collapse | Copy Code
@{
Layout = "another layout page";
}

RenderPage

What is RenderPage?

Renders the content of one page within another page. [MSDN] The page where you will place the content could be layout or normal page.

How RenderPage Works (graphical presentation)?

RenderPage Example

Create a page called _StaticRenderPage at Views/Shared folder. Open it and paste the below code.

 Collapse | Copy Code
<p>
This messge from render page.
</p>

Open the Index.cshtml file from Views/Home folder and paste the below code.

 Collapse | Copy Code
@RenderPage("~/Views/Shared/_StaticRenderPage.cshtml")

Now If you merge the code of _StaticRenderPage to Index.cshtml, then you will get the below code.

 Collapse | Copy Code
@{
ViewBag.Title = "Home Page";
} <h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
<p>
This messge from render page.
</p>

If you want to pass the data by using RenderPage, then you have to use the data parameter at RenderPage. I will give another example for this. To do this, at first create a class file called AvailableUser at Models/AccountModels. Create the class with the below code.

 Collapse | Copy Code
public class AvailableUser
{
public string UserName { get; set; }
public string UserPassword { get; set; } public static List<AvailableUser> AllUsers()
{
List<AvailableUser> userList = new List<AvailableUser>(); AvailableUser user1 = new AvailableUser
{
UserName = "Anupam Das",
UserPassword = "lifeisbeautiful",
}; AvailableUser user2 = new AvailableUser
{
UserName = "Chinmoy Das",
UserPassword = "GoodTime",
}; userList.Add(user1);
userList.Add(user2); return userList;
}
}

Now go to AccountController and write down the below code

 Collapse | Copy Code
public ActionResult AvailableUserList()
{
return View(MvcApplication1.Models.AvailableUser.AllUsers());
}

Create a view page called AvailableUserList.cshtml at Views/Account with the below code.

 Collapse | Copy Code
@model IEnumerable<MvcApplication1.Models.AvailableUser>

@{
ViewBag.Title = "AvailableUserList";
Layout = "~/Views/Shared/_Layout.cshtml";
} <h2>AvailableUserList</h2> @RenderPage("~/Views/Shared/_DisplayAllUsers.cshtml", new { AvailableUser = Model })

At last create another view page called _DisplayAllUsers at Views/Shared with the below code.

 Collapse | Copy Code
@foreach (var usr in Page.AvailableUser)
{
<text>
@usr.UserName @usr.UserPassword <br />
</text>
}

Now run the project (Account/AvailableUserList) and see the user list which comes from AvailableUser class.

RenderSection

What is RenderSection?

In layout pages, renders the content of a named section. [MSDN]

How RenderSection Works (graphical presentation)?

RenderSection Example

It’s simple, just add the below code at _layout page.

 Collapse | Copy Code
@RenderSection("Bottom",false)

and add the below code at Index page.

 Collapse | Copy Code
@section Bottom{
This message form bottom.
}

That’s all. But keep in mind that if you don’t want to use the Bottom section in all page then must use the false as second parameter at RenderSection method. If you will mention it as false then it will be mandatory to put Botton section at every content page.

Now run the project and see how it works !!!

I will be happy, if you found anything wrong or know more please share it via comments.

RenderBody, RenderPage and RenderSection methods in MVC 3的更多相关文章

  1. RenderBody,RenderPage和RenderSection

    1. RenderBody 在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到<body>标签里 ...

  2. [转]MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    本文参考自下面文章整理 MVC Razor模板引擎 @RenderBody.@RenderPage.@RenderSection及Html.RenderPartial.Html.RenderActio ...

  3. ASP.NET MVC之Layout布局与@RenderBody、@RenderPage、@RenderSection

    @RenderBody @RenderBody是布局页(_Layout.cshtml)通过占位符@RenderBody占用独立部分,当创建基于此布局页的试图时,视图的内容会和布局页合并,而新创建的视图 ...

  4. 《ASP.NET MVC4 WEB编程》学习笔记------RenderBody,RenderPage,RenderSection

    ASP.NET MVC 3 已经正式发布了,现在估计许多人都在拼命学,我也不能例外,刚刚看到了一篇文章,介绍了三个非常有用的方法:RenderBody,RenderPage和RenderSection ...

  5. Razor引擎学习:RenderBody,RenderPage和RenderSection

    ASP.NET MVC 3 已经正式发布了,现在估计许多人都在拼命学,我也不能例外,刚刚看到了一篇文章,介绍了三个非常有用的方法:RenderBody,RenderPage和RenderSection ...

  6. Mvc4_ActionLink跟@RenderBody ,@RenderPage

    . @Html.ActionLink("该链接要显示的文字A","对应的控制器方法B");会生成:<a href="C/B">A ...

  7. MVC Razor模板引擎 @RenderBody、@RenderPage、@RenderSection及Html.RenderPartial、Html.RenderAction

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  8. MVC View显示详解(RenderBody,RenderPage,RenderSection,Partial)

    一.Views文件夹 -> Shared文件夹下的 _Layout.cshtml 母版页 @RenderBody 当创建基于_Layout.cshtml布局页面的视图时,视图的内容会和布局页面合 ...

  9. MVC 中 Razor引擎学习:RenderBody,RenderPage和RenderSection

    RenderBody 在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到 标签里有这样一条语句: @Rend ...

随机推荐

  1. 工作那些事(四)大公司VS小公司

    都说大公司学做人,小公司学做事.这话有一定的道理,但是也不是绝对的.做人简单地说就是与人打交道,为人处世:做事,对我们来说就是实现需求,解决bug.有句话,是这么说的:有人的地方就有江湖,有江湖,就得 ...

  2. 快速优化yum (for centos5.5)

    定义yum超时时间:vi /etc/yum.conftimeout=120 修改源:(全部复制粘贴即可)cd /etc/yum.repos.d/mv rhel-debuginfo.repo rhel- ...

  3. 设为首页 收藏(IE可用)

    function SetHome(obj, vrl) { try { obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vr ...

  4. HTTP头信息解读

    本文为多篇“HTTP请求头相关文章”及<HTTP权威指南>一书的阅读后个人汇总整理版,以便于理解. 通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.客户端向服务器发 ...

  5. web项目环境搭建(1):建立一个maven项目

    一.maven简介以及常用概念 1.Maven是一个项目管理和整合的工具.Maven为开发者提供了一套完整的构建生命周期框架.开发团队基本不用花多少时间就能自动完成工程的基础构建配置,因为Maven使 ...

  6. rails 中 create, new, build, save 的用法以及误区汇总

    自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...

  7. MySQL是否在扫描额外的记录

    在确定查询只是返回需要的数据之后,接下来应该看看查询为了返回结果是否扫描了过多的数据.对于MySQL,最简单的衡量查询开销的三个指标如下: 1响应时间,2扫描行数,3返回行数 没有那个指明能够完美的衡 ...

  8. 关于asp.net会话阻塞

    现象:在一个网站中,当访问一个处理比较耗时的页面(A页面),页面请求还没有返回时,此时再点击访问该网站的其他页面(B页面)会出现B页面很久都没有响应和返回,直到A页面输出返回数据时才开始处理B页面的请 ...

  9. jquery keypress事件浏览器兼容性

    今天在做“财务管理系统”的时候,使用jquery的ajax从前台传递用户输入到后台,并保存到数据库,但是在前台为了界面的简介和一致性,没有使用按 钮来实现"确定"和"取消 ...

  10. Mysql主从复制的配置(双机互为主从)

    目的: 让两台mysql服务器可以互为主从提供同步服务. 优点: 1. mysql的主从复制的主要优点是同步"备份", 在从机上的数据库就相当于一个(基本实时)备份库. 2. 在主 ...