MVC的布局頁,視圖布局頁和分布頁的使用

來源:互聯網
上載者:User

標籤:全域   oct   頁面   main   art   dex   viewport   html   class   

一,結構如

二,布局頁和視圖布局頁

1》使用方法一

_ViewStart.cshtml

@{    Layout = "~/Views/Shared/_Layout.cshtml";}<h2>視圖布局頁_ViewStart.cshtml</h2>

_Layout.cshtml

<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>@ViewBag.Title</title></head><body>    <div>    <h2>布局頁_Layout.cshtml</h2>        @RenderBody()    </div></body></html>

綜上:

1》當使用這種寫法時:若視圖布局頁_ViewStart.cshtml不存在或者Layout = "~/Views/Shared/_Layout.cshtml";指向的分頁錯誤,都會造成視圖的布局頁(_Layout.cshtml)使用失敗

2》但是視圖布局頁_ViewStart.cshtml本身的樣式等一樣應用全域,並沒有失效

2》使用方法二(不使用視圖布局頁_ViewStart.cshtml)

_LayoutNew.cshtml

<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>@ViewBag.Title</title>    @RenderSection("Head", false)</head><body>    <div>        <h2>            布局頁_LayoutNew.cshtml</h2>        @RenderSection("Main", false)    </div></body></html>

hz.cshtml使用模板

@{    Layout = "~/Views/Shared/_LayoutNew.cshtml";}@section Head{}@section Main{    <h2>        hz頁面使用_LayoutNew.cshtml模板</h2>}

即:這樣應用模板一樣可以成功,在這裡需要注意的是@section Head{}@section Main{}名字需要和布局頁定義的一樣

三,分部頁的使用(不需要建立action,使用簡單 )

Index.cshtml

<h2>Index頁面</h2>@Html.Partial("_PartialView")

不需要使用控制器(Controllers)和方法(action),直接可以使用這個頁面的元素

MVC的布局頁,視圖布局頁和分布頁的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.