1

我已经开始学习 Asp.Net MVC,问题是当我添加一个名为Index.cshtml它的新视图时,它会自动从Layout Page. 我不知道这里发生了什么。

索引.cshtml:-

@{
   ViewBag.Title = "Index";
 }

 <h2>Index</h2>

布局页面:-

<!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>
4

3 回答 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

于 2014-08-21T04:55:14.887 回答
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

于 2014-08-21T05:41:11.977 回答
0

只需使您的布局为空,就不会呈现主布局

@{
   ViewBag.Title = "Index";
   Layout = null;
 }
于 2014-08-21T05:01:34.813 回答