Rex Whitten
Rex Whitten

Reputation: 735

ASP.NET MVC 4 - _Layout Master pages loading in views not configured to do so

In my ASP.NET MVC 4 application, I have created a view (not a partial) that shows only a grid of data for printing. This acts as a print-safe view. This is enabled by a simple tag that sends you to the view. The problem is, the master page layouts are being loaded in the view.

Is there anywhere in an ASP.NET MVC 4 app, that could be secretly configuring my views to include the master _layout?

Upvotes: 0

Views: 1235

Answers (3)

Rex Whitten
Rex Whitten

Reputation: 735

Since my _Layout was entirely in 1 element, i was able to use javascript to hide it. This not the answer I like, because its not a solution, it is a cheap work around.

Upvotes: 0

haim770
haim770

Reputation: 49095

Yes. check your ~/Views/_ViewStart.cshtml file, you may find something like this:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

You can override it in your View though:

@{
    Layout = null;
}

Or if your View is rendered through an Action, try to return PartialView() instead of View():

public ActionResult RenderGrid()
{
     ...
     return PartialView(); // return View without invoking _ViewStart.cshtml
}

Upvotes: 1

Jason P
Jason P

Reputation: 27012

If you have a non-partial view, and unless you override the layout, whatever is configured in _ViewStart.cshtml will be used. You can override this by adding this to your view:

@{
    Layout = null;
}

Upvotes: 0

Related Questions