View razors without layout

How come when I have Layout = null; in my opinion - does it still impose a default layout ?!

Is there any trick to stop this?

Here is my view without a layout:

 @{ Layout = ""; } <!DOCTYPE html> <html> <head> <title>Index</title> @{Html.RenderAction("Head", "Header");} </head> <body> <div> Home </div> </body> </html> 

Here is the output!

 <!DOCTYPE html> <html> <head> <title>Index</title> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script> </head> <body> header </body> </html> </head> <body> <div> Home </div> </body> </html> 
+42
layout asp.net-mvc asp.net-mvc-3 razor
Jul 07 2018-11-11T00:
source share
9 answers

Do you have _viewstart.cshtml in this directory? I had the same problem you encountered when trying to use _viewstart. Then I renamed it to _mydefaultview, moved it to the views / shared directory and switched to the absence of the view in the cshtml files where I don't want it, and specifying _mydefaultview for the rest. I don’t know why it was necessary, but it worked.

+6
Jul 07 '11 at 16:26
source share

I think:

 @{ Layout = ""; } 

does not match this:

 @{ Layout = null; } 

I use the second one and it works, not _Viewstart.

+70
Jul 16 '13 at 9:22
source share

You (and KMulligan) do not understand the _ViewStart pages.

_ViewStart will always execute before the start of your page.
It is intended to initialize properties (for example, Layout ); it should not contain markup at all. (Since there is no way to override it).

The correct template is to make a separate layout page that calls RenderBody , and set the Layout property to specify this page in _ViewStart .

You can then change the Layout on your content pages, and the changes take effect.

+28
Jul 07 '11 at 17:52
source share

The logic for determining whether a View should use a layout or not should NOT be in _viewStart or View . Setting a default value in _viewStart fine, but adding any layout logic in the / viewstart view does not allow this view to be used anywhere else (with or without a layout).

Your controller action should:

 return PartialView() 

By placing this type of logic in a presentation, you violate the rule of the principle of single responsibility in M (data), V (visual), C (logical).

+10
Mar 27 '14 at 18:59
source share

Using:

 @{ Layout = null; } 

to get rid of the layout specified in _ViewStart.

+7
Jul 20 '16 at 18:08
source share

I think it's better to work with separate β€œviews”, Im trying to move from PHP to MVC4, its really hard, but im on the right track ...

Answering your question, if you will work with individual pages, simply edit the _ViewStart.cshtml file

 @{ Layout = null; } 

One more tip if you get some CSS path issues ...

Put "../" in front of the URL

These are two problems that I get today, and I solve in this way!

Hello;

+4
Mar 27 '14 at 18:51
source share

I wanted to display the login page without a layout, and this works very well for me (this is the _ViewStart.cshtml file) You need to set ViewBag.Title in the controller.

 @{ if (! (ViewContext.ViewBag.Title == "Login")) { Layout = "~/Views/Shared/_Layout.cshtml"; } } 

I know this a little late, but I hope this helps some body.

+2
Jun 03 '16 at 10:03
source share

Just create a view as a partial view so you don't use a layout file.

+1
Aug 05 '13 at 23:52
source share

Procedure 1: Implement Layout Layout Using the _ViewStart File in the Root of the Views Folder

This method is the easiest way for beginners to control the rendering of layouts in your ASP.NET MVC application. We can identify the controller and display the layouts as a parser, to do this, we can write our code in the _ViewStart file in the root directory of the "Views" folder. The following is an example of how this can be done.

  @{ var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString(); string cLayout = ""; if (controller == "Webmaster") { cLayout = "~/Views/Shared/_WebmasterLayout.cshtml"; } else { cLayout = "~/Views/Shared/_Layout.cshtml"; } Layout = cLayout; } 

Procedure 2: set the layout by returning from ActionResult

One of the great features of ASP.NET MVC is that we can override the default layout rendering by returning the layout from ActionResult. Thus, it is also a way to render various layouts in an ASP.NET MVC application. The following code example shows how to do this.

 public ActionResult Index() { SampleModel model = new SampleModel(); //Any Logic return View("Index", "_WebmasterLayout", model); } 

Procedure 3: View-wise Layout (Defining a Layout in Each View from Above)

ASP.NET MVC provides us with such a wonderful feature and the ability to send faxes to override the default layout rendering by defining the layout on the view. To implement this, we can write our code as follows in each view.

 @{ Layout = "~/Views/Shared/_WebmasterLayout.cshtml"; } 

Procedure 4: Placing the _ViewStart File in Each of the Directories

This is a very useful way to set different layouts for each controller in your ASP.NET MVC application. If we want to set the default layout for each directory, we can do this by placing the _ViewStart file in each of the directories with the necessary layout information, as shown below:

 @{ Layout = "~/Views/Shared/_WebmasterLayout.cshtml"; } 
0
Aug 29 '17 at 5:28
source share



All Articles