簡體   English   中英

MVC 4 Razor _Layout.cshtml僅將HTML部分用於一頁

[英]MVC 4 Razor _Layout.cshtml use a HTML section for only one page

我有一種情況,我希望_Layout.cshtml中的特定內容僅用於一個視圖

“ _Layout.cshtml”看起來像這樣。

<div>
    //common part for all pages - 1
</div>

<div>
   //only for index page - want this only for index page.
   //but don't know how to specify this condition. if(it is an index page) then use this section
   //I can't move this section to my index page, 
   //because I have some html content Displayed on Index page from below section, i.e. common part-2.
</div>

<div>
    //common part for all pages - 2
</div>

  @RenderBody()

 <div>
     //common part for all pages - 3
</div>

我的index.cshtml頁面應如下所示。

<div>
    //common part for all pages - 1
</div>

<div>
    //only for index page
</div>

<div>
    //common part for all pages - 2
</div>

  //Content specific to index.cshtml

 <div>
     //common part for all pages - 3
</div>

其他所有頁面應如下所示。

<div>
    //common part for all pages - 1
</div>

<div>
    //common part for all pages - 2
</div>

  //Content specific to all other pages

 <div>
     //common part for all pages - 3
</div>

我該怎么辦? 我應該使用什么?

您可以在母版頁中設置非必填部分

@RenderSection("sectionforindex", required : false)

並在您的索引頁面中實施

@section sectionforindex
{
<div>
    //only for index page
</div>
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM