tags:

views:

366

answers:

4

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.

A: 

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.

Will
He doesn't want an empty result, he wants a result without a layout.
Matt
Wow, that was helpful. And a custom ActionResult wouldn't be able to do this?
Will
A: 

If you want to render no content to the output stream, just return an EmptyResult from your action instead of a ViewResult.

Richard Szalay
He doesn't want an empty result, he wants a result without a layout.
Matt
Ah, that edit was added later on.
Richard Szalay
A: 

Does your view need to have a masterpage at all - they are an optional feature of WebForms? Also, are you rendering html inside your view?

It doesn't make sense to render an ASPX page that was written with a masterpage in mind without the masterpage, as your Content controls are not necessarily in the correct order.

Richard Szalay
You can reuse views by assigning the masterpage at runtime. What the OP is asking is how to render the actual view out without rendering it's masterpage, presumably rendering the "Content" elements in the order they were placed in the ASPX.
Richard Szalay
Sorry, yes, my mistake.
Mauricio Scheffer
A: 

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.

Sohnee