1

I have started learning Asp.Net MVC and the Problem is that when i add a new view named Index.cshtml it automatically takes Html from Layout Page. I don't know what is happening here.

Index.cshtml :-

@{
   ViewBag.Title = "Index";
 }

 <h2>Index</h2>

Layout Page :-

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<p>This is a Layout Page....</p>
@RenderBody()

@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69

3 Answers3

6

Just include Layout = null and your problem will be solved as :-

Index.cshtml :-

@{
   ViewBag.Title = "Index";
   Layout = null;
 }

 <h2>Index</h2>

The Problem is because of the usage of the 'Layout' property, which if you do not specify a value explicitly will use the _ViewStart.cshtml file for it's layout. Specifying Layout=null in your view will cause it to ignore the layout specified in _ViewStart.cshtml file.

EDIT :-

if you want to know more about _ViewStart.cshtml and how it works then visit this link :-

Where and how is the _ViewStart.cshtml layout file linked?

OR

http://www.dotnetcurry.com/showarticle.aspx?ID=605

Community
  • 1
  • 1
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

There are a number of solutions to this problem. The first, as posted by @Exception is to use the Layout=null statement in your view.

You can also return a PartialView() in your controller. PartialView's do not render layouts, so even though your view is a full view, using the PartialView type will cause no layout to occur.

public ActionResult Index() {
    return PartialView("Index");
}

A 3rd option is to remove the _ViewStart.cshtml file from your project, if you don't plan on using any layouts at all.

A 4th option would be to use an alternate layout file for this action, if you simply want a layout that is different for this action, you can specify a different layout file using Layout="path-to-layout.cshtml";

You can also Nest layouts, which may help you to avoid creating these one-off pages in cases where you want slightly different layouts.

https://stackoverflow.com/a/7603129/61164

Community
  • 1
  • 1
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Hello..do you really think taking full view as partial view is a good idea...and for one view removing _viewstart file is not a good idea either... – Kartikeya Khosla Aug 21 '14 at 05:53
  • @Exception - The only difference between a View and PartialView is Layout rendering. PartialView forces layout to null, that's it. And if you aren't going to use layouts, there is no reason to have a _viewstart file. Why would you think it's not a "good idea" to get rid of something you aren't using? – Erik Funkenbusch Aug 21 '14 at 05:54
  • the user here has mentioned that he don't want to use layout the user only want to disable it for one view only... – Kartikeya Khosla Aug 21 '14 at 05:56
  • @Exception - you really need to read better. My post said explicitly "if you don't plan on using any layouts at all". I was simply laying out the options. – Erik Funkenbusch Aug 21 '14 at 05:57
  • okk..but in my opinion removing _viewstart.cshtml is not a great solution of the problem... – Kartikeya Khosla Aug 21 '14 at 05:59
  • @Exception - It's the perfect solution, but only *IF* you aren't going to be using any layouts at all. It's pointless if you aren't using them, and will only cause bugs. Plus it clutters up your project for something you aren't using. Again, only if you aren't using layouts. This was only one option, and I outlined a number of other options for using layouts that left _ViewStart intact. – Erik Funkenbusch Aug 21 '14 at 06:01
  • i don't think anyone don't use layouts while working in mvc because layouts are master page which is very important to give consistent look and feel to the web app and moreover in mvc all bundles for js and css are included in layouts only... – Kartikeya Khosla Aug 21 '14 at 06:04
  • @Exception - Some people are stubborn, and want to make each page stand alone. Some people want each page to look different. Some people want to specify their layout explicitly (and not rely on _ViewStart), or any of a number of other reasons... Here's another one, you have a server that's used only as an AJAX server, and never loads full pages... – Erik Funkenbusch Aug 21 '14 at 06:06
  • Okk..Great..Thanks...!!!!...but including same js and css files again and again on each view is a very very bad idea... – Kartikeya Khosla Aug 21 '14 at 06:06
  • @Exception - You're right, which is why you should only include the javascript files you need on any given page. Including all of them in the master page is horribly inefficient. – Erik Funkenbusch Aug 21 '14 at 06:11
  • yes..this may be the case with js files but not for css files because generally same css is used on every page(example bootstrap files) and if we include css bundles on every page then it will be horribly inefficient..!!!! – Kartikeya Khosla Aug 21 '14 at 06:13
  • @Exception - You don't seem to be thinking clearly here... the whole point here is that the user wants the page to be totally different, as such he's not including the same CSS. – Erik Funkenbusch Aug 21 '14 at 06:14
  • this is your own opinion but as far as i m understanding the user only want to exclude layout from one view only... – Kartikeya Khosla Aug 21 '14 at 06:16
  • @Exception - The point here is that you don't know what is needed on the server... for instance, we just rolled out an angular.js project that used standard html files for the pages (including css link and js script tags). It makes calls to an MVC backend which returns nothing but partials.. there are no layouts, css, javascript, images, or anything other than code on the server. In this case, there is never any need for a _ViewStart file, so it doesn't exist. The problem here is that you're just thinking too narrowly, and reject anything that doesn't fit what you expect. – Erik Funkenbusch Aug 21 '14 at 06:25
  • okk..sir..Great.!!..the are user just asked a simple question and your are just extending to angular.js and all...i understand your point thanks a lot...:) – Kartikeya Khosla Aug 21 '14 at 06:28
0

Just make your layout null, than it will not render master layout

@{
   ViewBag.Title = "Index";
   Layout = null;
 }
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28