Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 27, 2012 05:50 PM by ayanmesut
Member
9 Points
14 Posts
Nov 26, 2012 04:55 PM|LINK
Sth like
Page1: <body> <!-- header --> <!-- nav --> </body> Page2: <body> <!-- header --> <!-- nav --> </body> header-page: <body> ... </body> nav-page: <body> ... </body>
Is there a good way to do it in cshtml?
6 Points
4 Posts
Nov 26, 2012 05:29 PM|LINK
You can use RenderPage("_page_name_here.cshtml") for that
Or create layout pages and apply layot to pages that need it
You will find great help here
http://www.asp.net/web-pages
219 Points
85 Posts
Nov 26, 2012 06:37 PM|LINK
You can use Layout. Create a cshtml file, _SiteLayout.cshtml. Put your objects that you want to repeat in pages.
<div id="container"> <div id="header"> <a href="default.cshtml"><img src="Images/header.jpg"/></a> </div> <div id="navigation"> <a href="Default.cshtml">| Home |</a> <a href="physics.cshtml">| Physics |</a> <a href="maths.cshtml">| Mathematics |</a> <a href="gb.cshtml">| Guest Book |</a> </div> <div id="page-content"> @RenderBody() </div> <div id="footer"> copyright 2012 by MES </div> </div>
Here @RenderBody() is very important. When you use this layout in a page, that page will be places nto the RenderBody part.
So in which page you want to use this layout, go there and write:
@{ Layout = "~/_siteLayout.cshtml"; }
Nov 27, 2012 04:41 PM|LINK
Yep, it works. But if page1 has the "header" and "navigation", but page2 is going to have "header" only.
Does it mean I need to create another layout to make it ?
Nov 27, 2012 04:52 PM|LINK
please first mark as answer
Nov 27, 2012 05:09 PM|LINK
In the layout make the navigation as section:
@RenderSection(“navigation” , required: false)
And in which pages you want to show the navigation:
@section navigation{ <a href="Default.cshtml">| Home |</a> <a href="physics.cshtml">| Physics |</a> <a href="maths.cshtml">| Mathematics |</a> <a href="gb.cshtml">| Guest Book |</a> }
that's all
mark as answer too please
Nov 27, 2012 05:24 PM|LINK
where to put the @RenderSection ? in <body> ??
I tried. but the section is still displayed. Strange.
Nov 27, 2012 05:50 PM|LINK
@RenderSection is in the Layout
JerryJRXU
Member
9 Points
14 Posts
HOWTO re-use a block of header, nav bar in different pages?
Nov 26, 2012 04:55 PM|LINK
Sth like
Page1: <body> <!-- header --> <!-- nav --> </body> Page2: <body> <!-- header --> <!-- nav --> </body> header-page: <body> ... </body> nav-page: <body> ... </body>Is there a good way to do it in cshtml?
ZiggyK
Member
6 Points
4 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 26, 2012 05:29 PM|LINK
You can use RenderPage("_page_name_here.cshtml") for that
Or create layout pages and apply layot to pages that need it
You will find great help here
http://www.asp.net/web-pages
ayanmesut
Member
219 Points
85 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 26, 2012 06:37 PM|LINK
You can use Layout. Create a cshtml file, _SiteLayout.cshtml. Put your objects that you want to repeat in pages.
<div id="container"> <div id="header"> <a href="default.cshtml"><img src="Images/header.jpg"/></a> </div> <div id="navigation"> <a href="Default.cshtml">| Home |</a> <a href="physics.cshtml">| Physics |</a> <a href="maths.cshtml">| Mathematics |</a> <a href="gb.cshtml">| Guest Book |</a> </div> <div id="page-content"> @RenderBody() </div> <div id="footer"> copyright 2012 by MES </div> </div>Here @RenderBody() is very important. When you use this layout in a page, that page will be places nto the RenderBody part.
So in which page you want to use this layout, go there and write:
@{ Layout = "~/_siteLayout.cshtml"; }JerryJRXU
Member
9 Points
14 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 27, 2012 04:41 PM|LINK
Yep, it works. But if page1 has the "header" and "navigation", but page2 is going to have "header" only.
Does it mean I need to create another layout to make it ?
ayanmesut
Member
219 Points
85 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 27, 2012 04:52 PM|LINK
please first mark as answer
ayanmesut
Member
219 Points
85 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 27, 2012 05:09 PM|LINK
In the layout make the navigation as section:
And in which pages you want to show the navigation:
@section navigation{ <a href="Default.cshtml">| Home |</a> <a href="physics.cshtml">| Physics |</a> <a href="maths.cshtml">| Mathematics |</a> <a href="gb.cshtml">| Guest Book |</a> }that's all
mark as answer too please
JerryJRXU
Member
9 Points
14 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 27, 2012 05:24 PM|LINK
where to put the @RenderSection ? in <body> ??
I tried. but the section is still displayed. Strange.
ayanmesut
Member
219 Points
85 Posts
Re: HOWTO re-use a block of header, nav bar in different pages?
Nov 27, 2012 05:50 PM|LINK