user1929393
user1929393

Reputation: 4267

MVC how to include a view that does not have layout info

I'm trying to reference a view, but do not want all the header and footer that comes with the view because the viewstart references layout page. When I have a popup, it also shows the menu items in the header. Is there a way not to include the layout.cshtml?

Upvotes: 3

Views: 2179

Answers (3)

Tamim Al Manaseer
Tamim Al Manaseer

Reputation: 3724

Put this in the header of the view

@{ Layout = null; }

Or something like that

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

All you have to do is explicitly set layout to null.

@{ Layout = null; }

Upvotes: 3

Jay
Jay

Reputation: 6294

Layouts can be specified directly in the view or based on the caller.

For example if your ActionMethod returns like this:

return View();

The view will be rendered with the layout. However if the ActionMethod returns like this:

return PartialView();

Then the rendered view will not have a layout.

However, this can be overridden in the view itself. In your view, if ViewBag.Layout is null the layout will not be included. Inversely, if ViewBag.Layout has a value that layout will be used, reguardless of how the view is called. For this reason, most views do not set ViewBag.Layout directly, and leave it up the the caller to specify the intent.

Hope his helps.

Upvotes: 4

Related Questions