Search code examples
asp.net-mvcasp.net-mvc-3layoutrazor

Razor View Without Layout


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>

Solution

  • 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 the Views/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.