63

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.

2
  • 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?" Commented Oct 21, 2008 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. Commented May 8, 2009 at 15:09

11 Answers 11

117

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

 @{ Layout = ""; } 
Sign up to request clarification or add additional context in comments.

1 Comment

is this in the view? is there still no way to do this from the controller? Not that I care any more, just amusing :)
83

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.

1 Comment

This works in the Razor View, but not in the Controller, as Aaron originally asked about.
20

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"; } 

Comments

12

In the Controller action we can set the required layout.

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

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

Comments

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); } 

Comments

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.

Comments

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.

Comments

3

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

Comments

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.

1 Comment

He doesn't want an empty result, he wants a result without a layout.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.