码迷,mamicode.com
首页 > Web开发 > 详细

AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo

时间:2015-04-08 19:55:06      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:mvc4   asp.net   

HomeController.cs文件内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcLayoutTest.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Index2()
        {
            return View();
        }
        public ActionResult Index3()
        {
            return View();
        }
        public ActionResult Index4()
        {
            return View();
        }

    }
}

在Shared下面,新建一个Layout文件:_MyLayout1.cshtml:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
</head>
<body>
    <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>
<h2>我来自自定义Layout2</h2>
    <div>
        @RenderBody()
    </div>
</body>
</html>


Index.cshtml:

@{
    ViewBag.Title = "Index";
}

<h2>我是Index</h2>
@Html.ActionLink("Index2","Index2")
@Html.ActionLink("Index3","Index3")
@Html.ActionLink("Index4","Index4")

Index2.cshtml:

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>不使用Layout,我是Index2</title>
</head>
<body>
    <h2>不使用Layout,我是Index2</h2>
</body>
</html>
Index3.cshtml:

@{
    Layout = "~/Views/Shared/_MyLayout1.cshtml";
}
<h2>我是Index3</h2>

Index4.cshtml:

@{
    ViewBag.Title = "Index4";
    Layout = "~/Views/Home/_MyLayout2.cshtml";
}

<h2>我是Index4</h2>




AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo

标签:mvc4   asp.net   

原文地址:http://blog.csdn.net/vinglemar/article/details/44944639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!