0

When should a page use layout or render layout? I'm a little confused when I should create a page which the page using layout.

different between

@{
    Layout = null;
}

And

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Cros
  • 4,367
  • 9
  • 41
  • 47
Ade Zaenudin
  • 31
  • 1
  • 11
  • ViewBag.Title You have title in header on browser, Layout you have on page background and action from Your Layout if Layout is null then You don't have background and action from layout. – Sebastian Wąsik Jun 26 '14 at 06:24
  • first, you should some article about "what is mvc, what is layout, what is razor, what is masterpage ..." – AliRıza Adıyahşi Jun 26 '14 at 06:54

3 Answers3

1

_Layout is like a masterpage in ASP.NET webforms. If you want to apply a common theme (menu, styles ..) across many pages then _Layout file is the best place to put into.

You can have one single _Layout file for the entire application or one each for each specific module of the application.

_ViewStart file has the reference to the _Layout page used in the application.

If you use the below code then the respective page resets the layout to null and does not render the layout defined in _ViewStart. Code in _ViewStart file gets executed at the start of each View.

@{
   Layout = null;
}

If you want any specific layout in a page overriding the default layout defined int _ViewStart then you can do as below

@{
   ViewBag.Title = "Edit";
   Layout = "~/Views/Shared/_OtherLayout.cshtml";
}

You don't need to explicitly set the Layout in any of the individual view files unless if you want to override the default layout defined in _ViewStart.

Or if you want the _ViewStart to decide which layout page to render based on the controller then you can write something like below in _ViewStart page. The view will have the respective layout themes applied when rendered.

@{
    var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();

    string layout = "";
    if (controller == "ReportController")
    {
       layout = "~/Views/Shared/_ReportsLayout.cshtml";
    }
    else
    {
        layout = "~/Views/Shared/_Layout.cshtml";
    }

    Layout = layout;
 }
Dennis R
  • 3,195
  • 1
  • 19
  • 24
0

The _Layout is a template/framework/masterpage for all your views. It saves you from adding things like menus, sidebars, the html head or javascript/css includes on each and every new view you create. Use a layout page to take have "wrapping" html that is rendered around your views.

Mats
  • 14,902
  • 33
  • 78
  • 110
  • whether all pages must use layout? or not all pages which only displays an html that no use of javascript or other? – Ade Zaenudin Jun 26 '14 at 06:43
0

Ade,

I answered a similar question a while back:

*From scottgu's blog (ref: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx):

Starting with the ASP.NET MVC 3 Beta release, you can now add a file called _ViewStart.cshtml (or _ViewStart.vbhtml for VB) underneath the \Views folder of your project:

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default:

Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of our individual view files (except if we wanted to override the default value above).

Important: Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selection logic richer than just a basic property set. For example: we could vary the Layout template that we use depending on what type of device is accessing the site – and have a phone or tablet optimized layout for those devices, and a desktop optimized layout for PCs/Laptops. Or if we were building a CMS system or common shared app that is used across multiple customers we could select different layouts to use depending on the customer (or their role) when accessing the site.

This enables a lot of UI flexibility. It also allows you to more easily write view logic once, and avoid repeating it in multiple places.*

see Where and how is the _ViewStart.cshtml layout file linked? for details

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63