In this article we will learn about the three methods of MVC 3 and those are
RenderBody
, RenderPage
, andRenderSection
. We will learn by the following topics:RenderBody
- What is
RenderBody
? - How
RenderBody
works? RenderBody
Example
- What is
RenderPage
- What is
RenderPage
? - How
RenderPage
works? RenderPage
example
- What is
RenderSection
- What is
RenderPage
? - How
RenderPage
works? RenderPage
Example
- What is
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)?
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/Sharedfolder. 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.
<!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>
@{
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>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!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.
@{
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.
<p>
This messge from
render page.
</p>
@RenderPage("~/Views/Shared/_StaticRenderPage.cshtml")
_StaticRenderPage
to Index.cshtml, then you will get the below 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>
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.
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;
}
}
AccountController
and write down the below code
public ActionResult AvailableUserList() { return View(MvcApplication1.Models.AvailableUser.AllUsers()); }
@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.
@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.
@RenderSection("Bottom",false)
and add the below code at Index page.
@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.
I will be happy, if you found anything wrong or know more please share it via comments.
0 comments:
Post a Comment