create layout view in mvc ASP.NET MVC
https.www//:theitroad.com
To create a layout view in ASP.NET MVC, follow these steps:
- Right-click on the "Views" folder in the Solution Explorer and select "Add" -> "Layout Page".
- In the "Add New Item" dialog box, give the layout view a name, such as "_Layout.cshtml", and click the "Add" button.
- In the layout view, add the common HTML elements that you want to include on all pages, such as the header, navigation menu, and footer. For example:
<!DOCTYPE html> <html> <head> <title>@ViewBag.Title</title> <link rel="stylesheet" type="text/css" href="~/Content/Site.css" /> </head> <body> <div id="header"> # Welcome to my website </div> <div id="menu"> @Html.ActionLink("Home", "Index", "Home") | @Html.ActionLink("About", "About", "Home") | @Html.ActionLink("Contact", "Contact", "Home") </div> <div id="content"> @RenderBody() </div> <div id="footer"> © @DateTime.Now.Year MyWebsite.com </div> </body> </html>
- In the child views that use this layout, add the
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
directive at the top of the file to specify that this view should use the_Layout.cshtml
file as its layout. For example:
@{ ViewBag.Title = "Home Page"; Layout = "~/Views/Shared/_Layout.cshtml"; } ## Welcome to my website! <p>This is the home page.</p>
This will cause the contents of the child view to be inserted into the RenderBody()
method of the layout view, while the header, menu, and footer will be displayed consistently across all views that use this layout.