Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the layout defined in _ViewStart for certain views in ASP.NET MVC 3?

Is it possible to suppress the layout expressed in _ViewStart.cshtml using ASP.NET MVC 3 for certain views of an app.

I understand that I can define the layout programmatically in the controller action. Maybe passing in "" achieves that?

like image 737
cs0815 Avatar asked Mar 03 '13 15:03

cs0815


People also ask

What is the difference between _ViewImports Cshtml and _ViewStart Cshtml?

Code in the _ViewStart. cshtml file will only be run for non-layout pages. Code in the _ViewImports. cshtml file will be run for both layout and non-layout pages.

Which is the default file is included in the views folder index Cshtml _layout Cshtml _ViewStart Cshtml none of these?

_ViewImports. cshtml file is usually placed in the Views folder.

Can partial view have layout?

Yes, even in Partial View also we can set Layout property which turns Partial View as normal View with Layout .


2 Answers

You have two options

1) Use return PartialView() from controller, it won't take Layout from View start

2) Assign Layout = null,

 @{
     Layout = null;
  }

Check-out this interesting discussion and answer by marcind around this subject

like image 171
ssilas777 Avatar answered Sep 18 '22 15:09

ssilas777


In order not to apply a layout, just assign null to the Layout property in your view:

@{
    Layout = null;
}

<!DOCTYPE html>
...
like image 27
serge.karalenka Avatar answered Sep 21 '22 15:09

serge.karalenka