0% found this document useful (0 votes)
13 views5 pages

ASP.Net MVC Special Views Guide

This guide covers the creation and usage of Partial Views, Layouts, and Nested Layouts in ASP.Net MVC. It explains how to render partial views using helper methods, maintain UI consistency with layouts, and manage nested layouts for complex applications. Key methods and steps for creating and linking these components are provided for effective implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

ASP.Net MVC Special Views Guide

This guide covers the creation and usage of Partial Views, Layouts, and Nested Layouts in ASP.Net MVC. It explains how to render partial views using helper methods, maintain UI consistency with layouts, and manage nested layouts for complex applications. Key methods and steps for creating and linking these components are provided for effective implementation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Quick Guide - ASP.

Net MVC Special Views

Partial View
Used to render a portion of a view content
 It can be reusable
 Reduce the complexity of markup
 Increases the maintenance of markup
Helper Methods
@[Link](partial view name) – Renders the given partial view content on the referred view
@[Link](partial view name, model) – Renders the given partial view content on the referred
view and passes the model object to the partial view.
How to Create?
 Right click on Shared folder and select AddView
 Enter the partial view name
 Select the Create a strongly typed view if you want to model type to the partial view otherwise
leave as it is.
 Select the Create as a partial view and this is mandatory step to create Partial view.
 Following snapshot shows a sample partial view creation.

How to Use?
 Use @[Link](partial view name) helper methods to link the created partial view to any

Sreenivasan,[Link]@[Link]
Quick Guide - [Link] MVC Special Views

view.
 Use @[Link](partial view name,mode) helper methods to link a strongly typed partial
view.

Example1: for normal partial view

<section id="login">
@[Link]("_LoginPartial")
</section>

Example2: for strongly typed partial view

@foreach (var item in Model) {


<tr>
<!--Link the storngly type partial view-->
@[Link]("_StudentItem",item)
</tr>
}
Layout
Used to maintain the UI consistency across the application
 Razor introduced two layout concepts, web page body and web page section
 Web page body renders the content of referencing view content
 Web page body uses @RenderBody() method to render the child views content
 Web page section defines multiple named sections in view
 Named section’s content customized in the referencing view
 Web page section uses @RenderSection method to declare the named sections
 @section block customize the content of a named section in referencing view
 _Layout.cshtml/.vbhtml is the default Layout which created by [Link] MVC templates
 Layout property is set the layout for a view
 _ViewStart.cshtml/vbhtml assigns the default layout to all views
Helper Methods
@RenderBody – Renders the content of referencing view at where it is placed
@RenderSection(named section) – Renders the defined named section content at where it is placed
@RenderSection(named section,required) – Renders the defined named section content at where it is
placed, required flag make the named section as mandatory, if flag is true then its mandatory otherwise
the referencing view need not give definition for it
@section – This is a special razor block which used to define a named section in the referencing view
IsSectionDefined - This method checks whether the given named section defined in referencing view or
not.
How to Create?
 Right click on Shared folder and select AddView
 Enter the layout name
 Leave the Create a strongly typed view as unchecked state
 Leave the Create view as a Partial view as unchecked state
 Uncheck the Use a layout or master page
 Following snapshot shows a sample layout creation

Sreenivasan,[Link]@[Link]
Quick Guide - [Link] MVC Special Views

 Once layout created, use @RenderBody and @RenderSection methods in the layout as you
need. You can’t use @RenderBody method in the same layout more than one time.

How to Use?
Use can link the layout by using Layout property of a view; the following snippet shows the sample use
of layout.
@{
[Link] = "Admin Console -";
Layout = "~/Views/Shared/_Admin.cshtml";
}
Nested Layout
[Link] MVC provides an option to create a child layout within a layout
 A complex layout can split into multiple(level) nested layout
 Reduce the complexity
 Increase the maintenance of layout
How to Create?
 Right click on Shared folder and select AddView
 Enter the nested layout name
 Leave Template Engine as default

Sreenivasan,[Link]@[Link]
Quick Guide - [Link] MVC Special Views

 Leave Create a strongly typed view as unchecked


 Leave Create view as a Partial view as unchecked
 Select a layout for Use a layout or master page
 Following snapshot shows a sample nested layout creation

How to Use?
Linking nested layout is similar to normal layout; you use layout property to use a nested layout in a
view.
@{
[Link] = "Create - Student";
Layout = "~/Views/Shared/_AdminNestedLayout.cshtml";
}
Important Notes
Parent layout named sections will not carry forward to nested layout, so you need to re-declare each in
nested layout otherwise it will give you an exception.
The following snippet shows the re-declaration of named section in nested layout.
@section scripts{
<!-- re-declare the parent section to extend to referencing view -->
@RenderSection("scripts”, false)
}

Sreenivasan,[Link]@[Link]
Quick Guide - [Link] MVC Special Views

You can use IsSectionDefined before declaration or re-declaration of named section. The following
razor block shows the same use of this method.

@if (IsSectionDefined("Scripts"))
{
@section scripts{
<!-- re-declare the parent section to extend to referencing view -->
@RenderSection("scripts",false)
}
}

Sreenivasan,[Link]@[Link]

You might also like