MVC初体验-模板(11)
视图引擎分为两种:
如何添加布局页(针对空项目):
①一般在View文件夹下新建Shared文件夹(非必须,按照编码习惯),然后右键添加MVC 5 布局页(Razor)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> @RenderSection("script1") </head> <body> <div> @RenderBody() @RenderSection("bottom1") </div> </body> </html>
这里我添加了两个RenderSection
如何使用RenderSection呢?
②RenderSection用法
在HomeController控制器中的Index行为中我添加了如下视图,布局页引用之前添加的布局,然后使用Section如下所示
@{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_LayoutPage1.cshtml"; } <h2>Hello</h2> @section script1 { <script> </script> } @section bottom1 { <h2>这是底部信息</h2> }
End