Part 61 - Named sections in layout files in mvc

Let us understand sections in a layout file with an example. Please watch Parts 59 and 60 before proceeding. 

At the moment on all the views(Index, Create, Edit, Details & Delete), we see the same navigation menu as shown below. 
named sections in layout in mvc

Let us say we want to change the navigation menu dynamically. For example, if I am on the Edit view, then I want the navigation menu to contain links for List, Details and Deleteviews as shown below. 
rendersection in mvc 

Here are the steps to achieve this using sections in layout file
Step 1: Define "Menu" section in Edit view. To define a section, use @section followed by, the name of the section. The menu section, is going to display List, Details and Delete links.
@section Menu
{
    @Html.ActionLink("List", "Index"<br />
    @Html.ActionLink("Details", "Details"new { id = Model.Id }) <br />
    @Html.ActionLink("Delete", "Delete"new { id = Model.Id })
}

Step 2: Specify a location in layout file, where we want the "Menu" section to be rendered.
Rendering sections in layout file 

The above code that is marked in red, is very simple to understand. If you navigate to a view, and if there is a "Menu" section defined in that view, then that content will be injected, else, the default content that is specified in the layout file is used. 

For example, Navigate to Edit view. Since "Edit" view has got "Menu" section defined, the content from that section (i.e List, Details and Delete links ) will be displayed. 

Now navigate to "Delete" view. "Menu" section is not defined in this view, so default content from the layout file (i.e Index action link) will be displayed. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值