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>

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

15 Comments

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...
@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?
the user here has mentioned that he don't want to use layout the user only want to disable it for one view only...
@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.
okk..but in my opinion removing _viewstart.cshtml is not a great solution of the problem...
|
0

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

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

2 Comments

what if answer is same? .:P
if the answer is same then it will not be beneficial for user who has put the question over here..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.