Should calling PartialView() within an action use the layout?
Here's my Actions:
public ActionResult SomeAction()
{
if (Request.IsAjaxRequest())
return PartialView();
else
return View();
}
Here's my _ViewStart.cshtml:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Scenario #1: Action calls a view with no layout specified.
@{
//Layout = "~/Views/Shared/_SomeLayout.cshtml";
}
<h2>View!</h2>
- View's Result: The view is wrapped in a layout
- PartialView's Result: The view is not wrapped in a layout
This is backed up by this response from Darin Dimitrov.
Scenario #2: Action calls a view with a layout specified.
@{
Layout = "~/Views/Shared/_SomeLayout.cshtml";
}
<h2>View!</h2>
- View's Result: The view is wrapped in a layout
- PartialView's Result: The view is still wrapped in a layout
This also seems to be backed up by this other response from Darin Dimitrov. (NOTE: Even though his answer is a catch-all for AJAX requests, this was the response to a question where the OP had two views, one full and one partial.)
So on the first, Darin is explaining that if you don't want a layout, use PartialView(), but in the second one he is saying if you don't want a layout, then here's a workaround.
Can someone explain to me if there is something I'm missing or why it is this way. Regardless of what Darin has said, if I only set the layout in _ViewStart.cshtml
, then I can ignore it with PartialView(), but if I set another layout in the View itself, then I can't ignore it.
Does this make sense? Should I be able to ignore both layouts? If not, why?