I have a _SiteLayout.cshtml file which structures the site and has some C# code on it. How do I send the _SiteLayout's C# code to all of its subpages? I am using WebPages Razor 2.
Thank you in advance.
There are two methods for implementing a layout page:
Layout
command in the code section of every page, as outlined in Creating a Consistent Layout in ASP.NET Web Pages (Razor) Sites_PageStart.cshtml
file(Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites).In the latter case you could disable the layout page for a single page using Layout = null
in the code section.
Edit
If you want to pass data from a master page to a partial page, you must use the RenderPage() method instead of the RenderBody() method, but the structure of your site fully changes.
If, by example, you want to create a master page that extracts data from a table and passes them to a partial page that can change, your master page could be something like:
@{
var db = Database.Open("Northwind");
var data = db.Query(@"SELECT [Product Id], [Product Name],
[English Name] FROM Products ");
var page = "Partial.cshtml";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
@RenderPage(page, new {gridData = data})
</body>
</html>
The code in Partial.cshtml takes data from the Page.gridData variable and displays them in a WebGrid:
@{
var grid = new WebGrid(Page.gridData);
}
<div>@grid.GetHtml()</div>