2016-03-25 55 views
5

我正在查看很多的教程,從web-forms.master VS MVC主佈局。佈局和不同的內容和風格,複雜的MVC4應用程序

我遇到的問題是關於嵌入式全局文件與單個頁面自己的文件。

(文件=樣式和腳本)

邊記下了我的觀點我實現了一個小的命名慣例/規則,這樣的佈局,大師有一個LO後綴, 所以如果我的應用程序名爲「SADMIN」,然後我的佈局(主)-chtml將被命名爲:_SAdminLO.cshtml

和我的佈局,主人,我有:

(爲簡單起見)只是一個主要的酒吧 - 鏈接(使所有的頁面有「鏈接「top-bar)

這是主要的佈局

[textlink1] | [textlink2] | [textlink3] | [textlink4] .... 

然後我有索引頁(它的名字是的cPanel)

在我的索引

cpanel.chtml除了主要佈局的文字吧我添加了圖標...複製在 圖標菜單形式頂欄

[IMG]   [IMG] 
    page_name  page_name 

    [IMG]   [IMG] 
    page_name  page_name 

所以一起佈局主_SAdminLO.cshtml + cpanel.chtml - 320交織的「主頁」我的應用程序的

現在我有,是完全獨立於他們的行動

個人網頁,但他們需要的是最上面一欄的CSS + HTML,而不是cpanel(索引)

所以我的情況即:在 rightclick->查看源代碼,我可以看到我所有的網頁具有雙重html標籤 -

<html> + <head> + <body> markup of `LO` 

&

<html> + <head> + <body> markup of `individual.cshtml ` 

文件:

-master-

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 

     <link href='@Href("~/Content/css/GlobUtils.css")' rel='stylesheet' type='text/css' /> 

    <title>@ViewBag.Title</title> 
    @Scripts.Render("~/js")// C# bundle 

    @RenderSection("head", false) 
</head> 
<body id="bodid"> 
<h2>Cpanel</h2> 



    <div id="navbar"> 

     <ul id="nav"> 

      <div class="menu-main-container"> 
       <ul id="menu-main" class="menu"> 
        <li id="menu-item-0" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-0 current_page_item menu-item-0"> 
          @Html.ActionLink("Cpanel", "Cpanel", "ControlPanel") 
        </li> 
        <li id="menu-item-1" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-1 current_page_item menu-item-1"> 
          @Html.ActionLink("Templates Generator", "TemplateGenerator", "ControlPanel") 
        </li> 
        <li id="menu-item-2" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-2 current_page_item menu-item-2"> 
          @Html.ActionLink("Content Editor", "ContentEditor", "ControlPanel") 
        </li> 
        <li id="menu-item-3" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-3 current_page_item menu-item-2"> 
          @Html.ActionLink("Files Uploader", "FilesUploader", "ControlPanel") 
        </li> 
        <li id="menu-item-4" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-4 current_page_item menu-item-2"> 
          @Html.ActionLink("Code Dev", "CodeDev", "ControlPanel") 
        </li> 

       </ul> 
      </div> 
     </ul> 

    </div> 
    @RenderBody() 

</body> 
</html> 

someindividualpage.chtml

@{ 
    Layout = "~/Views/Shared/_EvProAdminLO.cshtml"; 
} 
@model MvcE.Models.IndexModel.CreatedTemplateJPacket 

<!DOCTYPE html> 
    @{ 
     ViewBag.Title = "TemplateGenerator"; 
} 
<html> 
<head> 
    <!--[if lt IE 9]> 
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> 
    <![endif]--> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <title>Template Generator</title> 

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" /> 


    <script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> 

    <link href='@Href("~/Content/css/Chrm_TemplateGenerator.css")' rel="stylesheet" type="text/css" /> 


</head> 
<body id="bodid"> 
    <div id="DivEditor"> 
     <h1 id="ContH"> ControlPanel - TemplateGenerator</h1> 
     <div class="container-fluid"> 



      <div> 
        <a class="btn btn-CtrlsTmplt" id="save" href="#">save</a> 
        <a class="btn btn-CtrlsTmplt" id="load" href="#">load</a> 
        <a class="btn btn-CtrlsTmplt" id="clear" href="#">clear</a> 
        <input type="text" id="scr1" class="input_Border_AndBGClear" /> 
      </div> 



      <hr/> 
    @*----------------------------------localData-------------------------------------*@ 
      <div id="localData"> 

       <input id="DataValue_FormFileds" type="hidden" /> 
      </div> 
     </div> 


    </div> 


    <div id="SaveNewTmpltWwrap"> 

    </div> 



     <script src='@Href("~/Js/jspart2.js")' type="text/javascript"></script> 

</body> 

+0

因爲佈局和主視圖頁面都有標籤,所以您有雙''。將它從視圖頁面中移除,然後在自定義(並非必需)部分中包含可變頭部內容'@section myCustomSection {...}' – Jasen

+0

@Jasen這個「individual.cshtml」視圖需要它自己的css + js調用,個人2也有...主佈局+索引 - 部分有其自己的,如果你可以發佈一個只有框架的代碼'Layoutfile' +它是'index'靈魂隊友'局部視圖'和'個人X的另一個' cshtml' –

+0

您可以簡單地從除佈局以外的其他頁面中移除頭部和身體標籤,並使用部分來加載其自己的CSS和js文件 – ssilas777

回答

5

也許我不是很理解,但似乎你可以使用自定義s ections。

_layout

<html> 
    <head> 
     <title>@ViewBag.Title</title> 
     @* common script, css *@ 
     <script src="jquery.js"></script> 

     @* variable script, css *@ 
     @RenderSection("customHead", required: false) 
    </head> 

    <body> 
     <!-- nav bar here --> 
     @RenderBody() 

     @RenderSection("footer", required: false) 
    </body> 
</html> 

SomePage的。CSHTML

@{ 
    Layout = "~/Views/Shared/_EvProAdminLO.cshtml"; 
    ViewBag.Title = "Some Page 1"; 
} 

@section customHead { 
    <script src="page1.js"></script> 
} 

@* no <html>,<head>,<body> *@ 

@* inserted into RenderBody() *@ 
<div id="page1content"> 
    ... 
</div> 

由於部分聲明required: false它會不會嘗試呈現任何東西,除非該視圖包含部分。

您可以更改每頁的部分內容。

SomePage2.cshtml

@{ 
    Layout = "~/Views/Shared/_EvProAdminLO.cshtml"; 
    ViewBag.Title = "Some Page 2"; 
} 

@section customHead { 
    <script src="page2.js"></script> 
} 

@section footer { 
    <div>footer</div> 
} 

<div id="page2content"> 
</div> 

,而不是多個佈局 - 你也可以使用局部視圖和兒童行爲做變量的內容呈現。

SomePage3.cshtml

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
    ViewBag.Title = "Some Page 3"; 
} 

@Html.Partial("widget") 
@{Html.RenderAction("UserInfo");} 

現在大部分的版面內容的被刪除,變成內容列入,而不是排除規則定義的一切。

我發現佈局頁面更少,使用partials更易於維護,頁面更容易理解通過查看查看頁面將呈現的內容。

+0

感謝所以現在我看到了整個圖片。 '@section customHead'調用'@RenderSection(「customHead」,必需:false)' –

+0

但是w8 ..這對於'somepage'來說是完美的'otherPage'和它自己的頭+腳本 –

+0

也許我需要類似這個鏈接...看起來過度殺人......或者你的簡單添加會做什麼? nested masters-layouts link [http://www.mikesdotnetting.com/article/164/nested-layout-pages-with-razor] –