61

In MonoRail you can just CancelLayout() to not render the layout. In ASP.NET MVC, the only way to affect the layout seems to be to pass the layout name into the View() method like View("myview", "mylayout"); only it seems that passing null or an empty string doesn't do what I'd want.

I ended up creating an empty layout that just rendered the content, but that seems silly.

"Not Render the layout" means exactly that. In the web forms view engine they call layouts "master pages". I want to render just my action's view and not surround it with the master page.

Aaron Jensen
  • 6,030
  • 1
  • 30
  • 40
  • If you can't explain exactly what you're looking for you're not going to get any other answers. What do you mean by "not render the layout?" –  Oct 21 '08 at 14:09
  • Having to create an empty layout may seem silly, but how else will the webforms engine know how to order to asp.net content sections? I feel this is more of a problem with the webforms viewengine that you're having. – Min May 08 '09 at 15:09

11 Answers11

111

In MVC 3, you can remove the master layout code with:

   @{
    Layout = "";    
    }
expdiant
  • 1,180
  • 1
  • 7
  • 8
  • is this in the view? is there still no way to do this from the controller? Not that I care any more, just amusing :) – Aaron Jensen Apr 27 '11 at 03:07
78

At the beginning of view add this:

@{
    Layout = null;
}

If you want style sheet to remain, you'll need to add reference to it in that view.

berzinsu
  • 915
  • 7
  • 12
18

To disable this for all pages, edit the _ViewStart.cshtml (in the root, under the 'Views' folder), and ensure that it contains the following:

@{
  Layout = null;
}

And to enable the template for any specific view, the following can be added to the .cshtml file for that view, to enable the template:

@{
  Layout = "~/Views/Shared/_Layout.cshtml";
}
Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
12

In the Controller action we can set the required layout.

return View("Index", "_LAYOUT_NAME", model);

https://stackoverflow.com/a/5161384/2039603

Community
  • 1
  • 1
TomCat
  • 431
  • 1
  • 8
  • 18
8

I see in the right answer it says that "It seems this was impossible in the version of ASP.NET MVC"

Which version are you using? Because I found the solution (I had the same issue) to your problem

So, to Disable Layout in the page, you should use:

@{
   Layout = null;
}

And, as suggested here, this could solve your problem:

public ActionResult Index()
{
  SampleModel model = new SampleModel();
  //Any Logic
  return View("Index", "_WebmasterLayout", model);
}
lat94
  • 511
  • 1
  • 5
  • 18
6

Instead of using a normal view, create a partial view. These can then be used on their own, which acts very much like CancelLayout() - or you can incorporate them into a view that references the Master Page, in which case it will be the full layout. They are also useful if you want to send back a partial HTML chunk in response to an AJAX request.

Fenton
  • 241,084
  • 71
  • 387
  • 401
4

Not having any luck trying to set the masterPage parameter to "" or null and returning a View (like I didn't)?

Then try this and use PartialView instead:

   public ActionResult Article(string id)
    {
        return PartialView("~/Areas/Store/Views/CustomerService/" + id);
    }

I needed to do this to load the contents of a view asynchronously from JS.

3

It seems this was impossible in the version of ASP.NET MVC I was asking about.

Aaron Jensen
  • 6,030
  • 1
  • 30
  • 40
0

You can create a custom ActionResult that does pretty much anything. The ActionResult controls what is sent back to the client as the response. It would be trivial to create a class that extends ActionResult that does nothing.

0

One alternative is to actually specify a layout but make that layout empty "_EmptyLayout.cshtml" that contains nothing or just a comment that says it contains nothing so later someone sees it as intended.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
0

If any of the methods work, you can as well try this Duo:

@{
    Layout = null;
    Layout = "";
 }

This worked for me after a long wait.

Venugopal M
  • 2,280
  • 1
  • 20
  • 30