Documente Academic
Documente Profesional
Documente Cultură
While creating Master Page Layout in ASP.NET MVC 5, you may encounter with @RenderBody,
@RenderPage, and @RenderSection . In this tutorial, you will learn all about these with complete
programming example.
@RENDERBODY
@RenderBody is used for injecting content from child page into the master page design. If there is
no named section in child page, the content will get displayed in RenderBody section.
Example:
//Layout Page
1 <div class="content">
2 @RenderBody()
3 </div>
//Child Page
1 @{
2 ViewBag.Title = "DarkLayoutPage";
3 Layout = "~/Views/Shared/_MyLayout.cshtml";
4 }
5
6
7 <h2>DarkLayoutPage</h2>
8 Hello CompShop Appliation. I am in RenderBody section because there is no named section for me.
Output
(https://www.completecsharptutorial.com/wp-content/uploads/2017/10/New-Picture-12.png)
@RENDERSECTION
@RenderSection is used for injecting content in the defined section. It allows you to specify a region
in Layout.
Note: Order doesn’t matter. You can use @section Footer before @section Note and the output
would be same.
You have noticed that I have used false parameter when created a section on the layout page.
@RenderSection("Footer", false)
false parameter denotes that Footer section is optional and It is your choice whether to use it or not. If
you use true parameter then it is compulsory to use this section in child page otherwise you will get
following error.
Output:
(https://www.completecsharptutorial.com/wp-content/uploads/2017/10/rendersection.png)
@RENDERPAGE
@RenderPage is used to call one page inside another page. For example, you have promotional
texts and photos, and you want to use it all on your page but the condition is that promotions have to
change frequently.
You just create a page containing promotional texts and photos and use this page wherever you want
to insert. When the web page will get rendered in the browser, it will render all the content of that
page. When you want to change promotional texts just change it in one place and it would reflect all
the pages.
Example:
1. Go to View Shared folder and create _StaticPromotionalPage .
2. Add following code into this page.
1 <div style="padding:10px; background-color:#f3f045; color:#dc1f36; position:inherit">
2 <strong>This is Promotional page</strong>
3 You can use this page to insert this content.
4 </div>
COMPLETE CODE
3. Controllers HomeController.cs
1 using System.Web.Mvc;
2
3 namespace CompShop.Controllers
4 {
5 public class HomeController : Controller
6 {
7 public ActionResult Index()
8 {
9 return View();
10 }
11
12 public ActionResult About()
13 {
14 ViewBag.Message = "Your application description page.";
15 return View();
16 }
17
18 public ActionResult Contact()
19 {
20 ViewBag.Message = "Your contact page.";
21 return View();
22 }
23
24 public ActionResult Enquiry()
25 {
26 return View();
27 }
28
29 public ActionResult Purchase()
30 {
31 return View();
32 }
33 public ActionResult DarkLayoutPage()
34 {
35 return View();
36 }
37 }
38 }
Output:
(https://www.completecsharptutorial.com/wp-content/uploads/2017/10/New-Picture-4-1.png)
SUMMARY:
In this chapter, you learned @RenderBody, @RenderSection, and @RengerPage in details with
complete programming example. In the next chapter, you will learn about Partial View Pages in
ASP.NET MVC5 (https://www.completecsharptutorial.com/asp-net-mvc5/adding-partial-views-pages-
in-mvc-5-with-example.php).
MORE ARTICLES
Complete Csharp
Tutorial
about 2 months ago
https://www.completecsharptutorial.co
m/…/retrieve-database-…
Retrieve Database Result in MVC
and Convert and Download Output
into PDF. Easy C# Example.