RenderBody, RenderPage and RenderSection methods in MVC 3

时间:2021-07-16 10:12:45

原文地址: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, RenderPage and RenderSection methods in MVC 3

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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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:

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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)?

RenderBody, RenderPage and RenderSection methods in MVC 3

RenderPage Example

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

RenderBody, RenderPage and RenderSection methods in MVC 3 Collapse | Copy Code
<p>
This messge from render page.
</p>

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

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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.

RenderBody, RenderPage and RenderSection methods in MVC 3 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)?

RenderBody, RenderPage and RenderSection methods in MVC 3

RenderSection Example

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

RenderBody, RenderPage and RenderSection methods in MVC 3 Collapse | Copy Code
@RenderSection("Bottom",false)

and add the below code at Index page.

RenderBody, RenderPage and RenderSection methods in MVC 3 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&comma;RenderPage和RenderSection

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

  2. &lbrack;转&rsqb;MVC Razor模板引擎 &commat;RenderBody、&commat;RenderPage、&commat;RenderSection及Html&period;RenderPartial、Html&period;RenderAction

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

  3. ASP&period;NET MVC之Layout布局与&commat;RenderBody、&commat;RenderPage、&commat;RenderSection

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

  4. 《ASP&period;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&lowbar;ActionLink跟&commat;RenderBody &comma;&commat;RenderPage

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

  7. MVC Razor模板引擎 &commat;RenderBody、&commat;RenderPage、&commat;RenderSection及Html&period;RenderPartial、Html&period;RenderAction

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

  8. MVC View显示详解&lpar;RenderBody&comma;RenderPage&comma;RenderSection&comma;Partial&rpar;

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

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

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

随机推荐

  1. opencv5-objdetect之级联分类器

    这是<opencv2.4.9tutorial.pdf>的objdetect module的唯一一个例子. 在opencv中进行人脸或者人眼 或者身体的检测 首先就是训练好级联分类器,然后就 ...

  2. Twisted网络编程入门

    Twisted是用Python实现的基于事件驱动的网络引擎框架,功能非常丰富,基本包括了常用的网络组件. 所谓事件驱动,就是说程序就像是一个报警器(reactor),时刻等待着外部事件(event), ...

  3. ORACLE OCP认证

    基本情况介绍 Oracle产品非常多,这里说的是Oracle数据库认证体系. Oracle数据库认证体系包括3层,分别是OCA(助理),OCP(专家),OCM(大师) 一般情况下,需一级一级认证,也就 ...

  4. JS算法之快排&amp&semi;冒泡

    1.快速排序思想: 1.1 先找数组的最中间的一个数为基准 1.2 把数组通过此基准分为小于基准的left数组和大于基准的right数组, 1.3 递归重复上面的两个步骤, 代码如下: functio ...

  5. Windows窗口的尺寸和位置

    介绍 窗口的大小和位置表示为一个矩形边界,该矩形的坐标是相对于屏幕或父窗口而言的.*窗口的坐标是相对于屏幕的左上角而言的,子窗口的坐标则是相对于父窗口的左上角而言.应用程序创建窗口时(CreateW ...

  6. shell的case语句

    case语句格式 # vi test.sh : echo "input : " read num echo "the input data is $num" c ...

  7. Struts2中的数据处理的三种方式对比(Action中三种作用域request,session&comma;application对象)

    1:在Action中如何获得作用域(request,session,application)对象: 取得Map(键值对映射集)类型的requet,session,application; 对数据操作的 ...

  8. POJ2456 Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  9. DOM之节点类型加例子

    DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个HTML或XML文档的常用方法.D ...

  10. 我的shell脚本

    问题:在ip.lt文件中有600个IP,有3个文档模版,三个文档的名称结构都是“ip+一系列字符串”,要求:1.将600个IP分成3分,以三个模版为基础创建600个文档,名字结构与模版相同:2修改60 ...