MVC 4 Razor _Layout.cshtml只使用一个HTML页面

Ami*_*mit 1 c# asp.net-mvc razor asp.net-mvc-4

我有一个场景,我希望_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>
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

所有其他页面应如下所示..

<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>
Run Code Online (Sandbox Code Playgroud)

我该怎么办?我该怎么用?

Sye*_*qas 7

您可以在母版页中创建非必需部分

@RenderSection("sectionforindex", required : false)
Run Code Online (Sandbox Code Playgroud)

并在您的索引页面中实现它

@section sectionforindex
{
<div>
    //only for index page
</div>
}
Run Code Online (Sandbox Code Playgroud)