110 likes | 133 Views
Layout and Partial Views. MIS 324 Professor Sandvig. Overview. Layout View Define page layout once Inject dynamic content Partial Views Define “part of page” Use on multiple pages. Goal. Reusable UI code. Classes. Code. Reusability Modularity. Interface. Layout & Partial Views.
E N D
Layout and Partial Views MIS 324 Professor Sandvig
Overview • Layout View • Define page layout once • Inject dynamic content • Partial Views • Define “part of page” • Use on multiple pages
Goal • Reusable UI code Classes Code • Reusability • Modularity Interface Layout & Partial Views
Layout View Source: tutorialsTeacher.com
Layout View • Layout view contains shared UI • Current view injected: • @RenderBody() • Been using Layout View • Views folder • _ViewStart.cshtml
Layout View • Three ways to specify which layout to use • _ViewStart.cshtml • Layout Razor tag in each page • In ActionMethod
Layout View _ViewStart.cshtml in View folder
Layout View Action Method public ActionResult Index() { return View("Index", "_myLayoutPage"); }
Partial Views • Reusable UI code • Headers, footer, navigation, etc. • Create like any other view • Check Partial View • Adds: Layout = null; • Add to layout • @Html.Partial("_header")
Partial Views • Can fire action methods for dynamic content • @Html.Action("_leftMenu") • Example: Layout and Partial Views
Summary • Benefits of Layouts & Partial Views • Reusable UI code • Easy to use