ASP.NET MVC Layout Nesting
Sections appear to catch people out as they need to be
declared in the Layout specified by the content page, so if you have a
sub-layout calling a master layout, how do you pass over a scripts section from
the content to the master layout. This might catch a couple of people out who
consider themselves proficient in MVC. The answer is as simple as nesting the
sections too.
It will be good to adhere to the scripts section as many people who deal with layouts will be used
to that. Let consider a site that deals with Employee Management. The Employees
are of different categories and let’s say we have a controller dedicated to the
topic of Employee Department.
If we designed a page for HR department and it layout is
completely different from that of Customer Service department and Finance
Department.
We decided to use a stylesheet just for HR Department
area of the site – no Customer or Finance styles.
What we should always put
at the back of our minds when dealing with Nested Layout in ASP.NET MVC is the
scripts section has to be nested also so as to have the effects of the scripts
in the child layout.
As explained above, if we
have HR department page with some menu or different visual effects which is
specific to that layout and the general layout of the entire site, we can have
all the scripts section defined in the master layout, that is, the jquery and
CSS links in the script section of the master layout and nest the script
section the HR specific Layout as shown below
By nesting Layout in a view is to called a
master layout within the head section of the child layout as shown below
In the master layout, we
have to provide a script section as shown below
Called the child layout
in our view mean to specify the location of the layout in our view as shown
below
In the child layout, we
also nest the script section as shown below
Complete Master Layout code
complete Nested Layout code
I love the way you explained the concept of layout nesting Engineer. Thank you
ReplyDeleteYou are welcome
Delete