In this article we will see how to create a layout for a view in MVC.
Create a MVC project from the "Empty" template.
Right-click on "Controllers" and select "Add" >> "Controller...".
Select "MVC 5 Controller - Empty" to add an empty controller. Click on the "Add" button.
Name the controller as in the following:
Now we need to create a view.
Right-click on "Index" and select "Add View...".
Name the view and select "Empty (without model)" as the template.
Click on the "Add" button.
Right-click on "Views" and select "Add" >> "New Folder".
Name the folder as "Shared", this will create "Shared" folder under the view.
Right-click on the "Shared" folder and select "Add" >> "View…"
Name the view as "_Layout" and select "Empty (without model)" as the template.
Design your desired layout in _Layout.cshtml.
Here we create a header, footer, side bar and content area. The header, footer and side bar should be shown in all pages, so we create them in _Layout.cshtml. The variable portion of the site is the content area, where content is changed dynamically. In ASP.NET we use a "ContentPlaceHolder", in the same way in MVC we use "RenderBody()".
Attach a layout page to Index.cshtml. Set the link of _Layout.cshtml in Index.cshtml.
Add the content in the body part of Index.cshtml.
Run the project and you can see that the Index view has been rendered with the layout.