How come when I have Layout = null;
in my view - it still pulls in the default layout?!
Is there some trick to stop it doing that?
Here is my view without layout:
@{
Layout = "";
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
@{Html.RenderAction("Head", "Header");}
</head>
<body>
<div>
Home
</div>
</body>
</html>
Here is the rendered output!!
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
header
</body>
</html>
</head>
<body>
<div>
Home
</div>
</body>
</html>
Do you have a
_ViewStart.cshtml
in this directory? I had the same problem you're having when I tried using _ViewStart. Then I renamed it _mydefaultview, moved it to theViews/Shared
directory, and switched to specifying no view in cshtml files where I don't want it, and specifying _mydefaultview for the rest. Don't know why this was necessary, but it worked.You (and KMulligan) are misunderstanding
_ViewStart
pages._ViewStart
will always execute, before your page starts.It is intended to be used to initialize properties (such as
Layout
); it generally should not contain markup. (Since there is no way to override it).The correct pattern is to make a separate layout page which calls
RenderBody
, and set theLayout
property to point to this page in_ViewStart
.You can then change
Layout
in your content pages, and the changes will take effect.Logic for determining if a View should use a layout or not should NOT be in the
_viewStart
nor theView
. Setting a default in_viewStart
is fine, but adding any layout logic in the view/viewstart prevents that view from being used anywhere else (with or without layout).Your Controller Action should:
By putting this type of logic in the View you breaking the Single responsibility principle rule in M (data), V (visual), C (logic).
I think it's better work with individual "views", Im trying to move from PHP to MVC4, its really hard but im on the right way...
Answering your question, if you'll work individual pages, just edit the _ViewStart.cshtml
Another tip if you're getting some issues with CSS path...
Put "../" before of the url
This are the 2 problems that i get today, and I resolve in that way!
Regards;
I wanted to display the login page without the layout and this works pretty good for me.(this is the _ViewStart.cshtml file) You need to set the ViewBag.Title in the Controller.
I know it's a little bit late but I hope this helps some body.
I think this :
is not the same as this :
I use the second and it's working, no _Viewstart included.