Click here to Skip to main content
11,641,659 members (65,586 online)
Click here to Skip to main content

Tagged as

RenderBody, RenderPage and RenderSection methods in MVC 3

, 12 Mar 2013 CPOL 174.7K 44
Rate this:
Please Sign up or sign in to vote.
In this article we will learn about the three methods of MVC 3 and those are RenderBody, RenderPage, and RenderSection.

In this article we will learn about the three methods of MVC 3 and those are RenderBody, RenderPage, and RenderSection. 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…


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.

<!DOCTYPE html>

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

    ViewBag.Title = "Home Page";


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.

    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.

<!DOCTYPE html>
<title>Home Page</title>

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";


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.

This messge from render page.

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


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

    ViewBag.Title = "Home Page";


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.

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",


            return userList;

Now go to AccountController and write down the below code

public ActionResult AvailableUserList()
return View(MvcApplication1.Models.AvailableUser.AllUsers());

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

@model IEnumerable<MvcApplication1.Models.AvailableUser>

ViewBag.Title = "AvailableUserList";
Layout = "~/Views/Shared/_Layout.cshtml";


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

@foreach (var usr in Page.AvailableUser)
@usr.UserName   @usr.UserPassword <br />

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


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.


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.

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sadeque Sharif
Software Developer Meridian Group
Bangladesh Bangladesh
I am:
Manager (IT) at Meridian Group.

I was:
Assistant Manager (Software Division) at KDS Garment Industries Limited.
Assistant Manager (Software Division) at E-Vision Software Limited.

My blog:

My preferred work area:

My email:

Follow me:
twitter | facebook | linkedin

You may also be interested in...

Comments and Discussions

Generalrendersection Pin
Rakesh kumar Behera2-Oct-14 18:17
memberRakesh kumar Behera2-Oct-14 18:17 
QuestionSmall Correction Pin
kumarbs23-Jul-14 20:11
memberkumarbs23-Jul-14 20:11 
Questionnice article Pin
samthec5-Jan-14 2:06
membersamthec5-Jan-14 2:06 
QuestionShort and simple, 5 stars Pin
Janilane18-Dec-13 16:28
memberJanilane18-Dec-13 16:28 
GeneralA Ray of Light in the darkness Pin
csugden22-Nov-13 3:42
membercsugden22-Nov-13 3:42 
GeneralNice Article Pin
JoCodes12-Nov-13 19:58
memberJoCodes12-Nov-13 19:58 
GeneralRe: Nice Article Pin
Sadeque Sharif12-Nov-13 21:20
professionalSadeque Sharif12-Nov-13 21:20 
GeneralMy vote of 5 Pin
Antariksh Verma4-Sep-13 22:38
professionalAntariksh Verma4-Sep-13 22:38 
GeneralMy vote of 5 Pin
nocksos29-Aug-13 21:49
membernocksos29-Aug-13 21:49 
GeneralMy vote of 5 Pin
techno2mahi16-Jul-13 23:14
membertechno2mahi16-Jul-13 23:14 
AnswerBlog Pin
Member 1003435011-Jun-13 1:11
memberMember 1003435011-Jun-13 1:11 
GeneralRe: Blog Pin
Sadeque Sharif11-Jun-13 1:15
memberSadeque Sharif11-Jun-13 1:15 
GeneralMy vote of 4 Pin
Chandrasekara Reddy10-Jun-13 3:57
memberChandrasekara Reddy10-Jun-13 3:57 
GeneralRe: My vote of 4 Pin
Sadeque Sharif10-Jun-13 23:20
memberSadeque Sharif10-Jun-13 23:20 
GeneralMy vote of 3 Pin
Donsw22-May-13 10:51
memberDonsw22-May-13 10:51 
QuestionPicture links broken Pin
kelvin199711-Mar-13 13:08
memberkelvin199711-Mar-13 13:08 
AnswerRe: Picture links broken Pin
Sadeque Sharif12-Mar-13 1:08
memberSadeque Sharif12-Mar-13 1:08 
GeneralMy Vote of 4 Pin
deepakaitr123458-Jan-13 19:48
memberdeepakaitr123458-Jan-13 19:48 
QuestionGood article Pin
Béla Tamás9-Dec-12 0:08
memberBéla Tamás9-Dec-12 0:08 
However, I think you have a small mistake in your last paragraph
If you will mention it as false true then it will be mandatory to put Botton section at every content page."
GeneralMy vote of 4 Pin
quangnd.edu13-May-12 22:42
memberquangnd.edu13-May-12 22:42 
GeneralRe: My vote of 4 Pin
Sadeque Sharif14-May-12 0:56
memberSadeque Sharif14-May-12 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 12 Mar 2013
Article Copyright 2012 by Sadeque Sharif
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid