I have a layout page (or master page) with MVC 3 and would like to make my views sort of content pages to this layout page, but alas I have no clue how to do this
I am totally new to MVC 3 and this may sound as a stupid question but I can't find anything relating to this.
In your view code, you would simply set the layout file. Assuming your Layout is named _Layout.cshtml and stored in /Views/Shared, the code might be:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
in MVC 3 we can set the layout page for the current view by doing this one
#{
ViewBag.Title = "Home page";
Layout = "../Shared/_Layout.cshtml";
}
if application need the same layout everywhere in application you can set them for whole application by making a _ViewStart.cshtml in views folder and place this one.
#{
Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
first code can be apply in the views for setting the layout for page only you have. or second can be apply in _ViewStart.cshtml in Views directories of your application.
Also, when you create new view you can select a "master page" for it. By default it's whatever "_ViewStart.cshtml" is linking to.
Related
In my View folder I have an _ViewStart.cshtml file where I have a default layout
Layout = "~/Views/Shared/_Layout.cshtml";
In my shared folder I have 2 files _Layout.cshtml and _LayoutLogin.cshtml. On my login view I want to use _LayoutLogin file, so I have done this in the view :
#{
Layout = "~/Views/Shared/_LayoutLogin.cshtml";
}
<h2>Login</h2>
But it stil use the _Layout file. How do I use another layout file, I have search on the internet for souloutions but nothing is working at all.
Hope someone could help me, I am going crazy!!
When you call the view from the controller you can specify a layout.
return View("Index", "_LayoutLogin", viewModel);
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";
}
_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;
}
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
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.
I have a _ViewStart.cshtml in my main view folder with the following code.
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
In the View folder of my admin area I have a _ViewStart.cshtml with this code
#{
Layout = Request.IsAjaxRequest() ? null : "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
I put a breakpoint on both lines of code, and when I go into the admin area I can see it hits the viewstart in that folder and not the one in the main folder. Even though it hits that code it still only uses the main layout.
I didn't see it at first, but the specific view I was testing with was specifying the wrong layout, so no matter what I put anywhere else it was overwriting it.
In default mvc app. There are Layout and content pages, You know (_Layout, Home, Contact, etc.)
And content pages do not contain layout refrence as this:
Layout = "~/Views/Shared/_Layout.cshtml";
In content pages this code is missing.
But they works. How does it do this without layout refrence?
Because your _ViewStart.cshtml contains a reference to the default layout that will be used when a specific one is not stated on the view.
When you want to change the layout for a single view, you would include a Layout = "..."; to that view.
If you want folder specific layouts i.e. (Home, Account, Product etc), you can put _ViewStart.cshtml in that folder & point out whichever layout tobe used in that file & it will override the root level layout.
Find ScottGu's blogs for more details on layouts & sections here & here
In MVC 3 Beta, is there a difference between the templates MVC 3 Partial Page (Razor) and MVC 3 View Page with Layout (Razor) ?
I added a partial page (_partialList) to my application. Now when I return only the partial view, it applies the Layout present in _ViewStart.cshtml - acting very much like a stardard view page with layout.
if (Request.IsAjaxRequest())
return View("_partialList", someModelData);
How does a "partial" page distinguish itself from a standard view page with layout ? Will the two behave differently in any particular scenario?
If you don't want to apply the layout return a PartialView instead of View:
if (Request.IsAjaxRequest())
return PartialView("_partialList", someModelData);
Darin's response solves your practical issue of not wanting the layout to be applied.
Regarding the difference between the two, in Razor they are practically the same because both full pages and partials use the same extension and have the same base class.
The reason why there is different UI is because in the Web Forms view engine the two are implemented with different extensions and different base classes, which is why to seperate templates are necessary.
Add the following code to your page, and the view engine will not apply the layout to it.
#{
Layout = null;
}
Views have this
#{
View.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
and partial views don't
I don't think there is any difference.