MVC4 利用Layout的几种方法的Demo
来源:互联网 发布:nginx http2配置 编辑:程序博客网 时间:2024/06/03 17:09
本文参考自 http://blog.csdn.net/vinglemar/article/details/44944639
因为 从网上下了个后台权限源码,想自己加上前台页面。发现了自己的不足,对于layout 的理解缺失。下面详细解释如何使用不同 的布局页。
页面不多,直接贴代码行了。
HomeController.cs
public ActionResult Index() { return View(); } public ActionResult Index2() { return View(); } public ActionResult Index3() { return View(); } public ActionResult Index4() { return View(); } public ActionResult Index5() { return View("Index5", "_MyLayout1"); }
在Shared下面,新建一个Layout文件:_MyLayout1.cshtml:
<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title></head><body style="background-color:#808080;"> <h2>我来自自定义Layout1</h2> <div> @RenderBody() </div></body></html>
在Home文件夹下新建Layout文件:_MyLayout2.cshtml:
<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title></head><body style="background-color:yellow;"> <h2>我来自自定义Layout2</h2> <div> @RenderBody() </div></body></html>
Index.cshtml:
@{ ViewBag.Title = "Index1";}<h2 style="background-color:red;">我是Index1</h2>@Html.ActionLink("Index2", "Index2")@Html.ActionLink("Index3", "Index3")@Html.ActionLink("Index4", "Index4") @Html.ActionLink("Index5", "Index5") //@Html.ActionLink("Index6", "Index6")
Index2.cshtml:
@{ //ViewBag.Title = "Index2"; Layout = null; }<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>不使用Layout,我是Index2</title></head><body style="background-color:green;"> <h2>不使用Layout,我是Index2</h2></body></html>
Index3.cshtml:
@{ Layout = "~/Views/Shared/_MyLayout1.cshtml";}<h2 style="background-color:blue;">我是Index3</h2>
Index4.cshtml:
@{ ViewBag.Title = "Index4"; Layout = "~/Views/Home/_MyLayout2.cshtml";}<h2 style="background-color:orange;">我是Index4</h2>
Index5.cshtml:
@{ ViewBag.Title = "Index5"; Layout = "~/Views/Home/_MyLayout2.cshtml";}<h2 style="background-color:#fff;">我是Index5</h2>
0 0
- MVC4 利用Layout的几种方法的Demo
- AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo
- AspNet MVC4 教学-19:Asp.Net MVC4 利用Linq技术的搜索应用快速Demo
- 动态layout的demo
- 几种非常好的log4j的layout
- CodeProject上的几种Layout
- Android几种layout(布局)的区别
- AspNet MVC4 教学-8:AspNet MVC4 4种引入CSS的方法
- 利用javascript实现页面跳转的几种方法
- 吴石:几种软件缺陷的可能利用方法
- 利用javascript实现页面跳转的几种方法
- 利用javascript实现页面跳转的几种方法
- 总结利用SEO赚钱的几种方法
- 利用Jsp下载文件的几种方法
- 几种软件缺陷的可能利用方法
- 单片机利用软件抗干扰的几种滤波方法
- 利用javascript实现页面跳转的几种方法
- 几种软件缺陷的可能利用方法
- PopWindow使用透明的View却出现布局为黑色的情况
- Java基础知识整理(一)
- python
- [初学 Unity]2D Game Creation-01.Unity For 2D
- 最大熵学习笔记(五)最优化算法
- MVC4 利用Layout的几种方法的Demo
- Andoid NDK编程 2 - JNI签名规则
- HDU 1029 Ignatius and the Princess IV
- STM32的PWM波的频率和占空比
- Eclipse历代版本
- 清除行列
- 为什么T—Shirt Sizes的评估,对长期来说不好?
- c++中的变量做数组长度
- day 1 HTML+JS 写钟表