Layouts in MVC

Layouts are used when there is a need to maintain a consistent look across multiple views.(Example : Header and footer across views).They are similar to master pages in ASP.NET.

Important Methods in Layouts :

@RenderBody():

1.Renders View and all of its components in layout page.

2.There can be only one RenderBody in a layout page.

Capture

@Renderpage():

1.Renders the specified page in layout.It takes an argument that specifies the location of page.


@RenderPage("~/Views/yourpage.cshtml");

2.There can be multiple RenderPage method in layout.

@Sections:

Renders the content of page in specified region  in layout.


@section("nameofsection,false")

1.Requires name of the section.If not specified exception will be thrown.

2.IsRequired attribute in section method has to be set to false,if section is not mandatory for every view,else an exception will be thrown.

rendersection

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s