文件“~/Views/Position/Edit.cshtml"不能直接请求,因为它调用了“RenderSection"方法 [英] The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method

查看:33
本文介绍了文件“~/Views/Position/Edit.cshtml"不能直接请求,因为它调用了“RenderSection"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有可以重复使用的东西分开,这样我会更容易维护.

I am trying to separate all the things that I could reuse in sections, so it would be easier for me to maintain.

但是我得到了这个例外:无法直接请求文件~/Views/Position/Edit.cshtml",因为它调用了RenderSection"方法

However I got this exception: The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method

我创建了一个名为sections.cshtml的文件,内容如下:

I created a file called sections.cshtml with the following content:

@section scripts{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

在 _layout.cshtml 文件中,我将其更改为:

And in the _layout.cshtml file I changed it to:

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    @RenderSection("scripts", required:false)
    @*<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>*@
</head>

当我转到浏览器中的视图并检查它只显示的源代码时:

When I go to the view in the browser and check the source code it shows only:

<head>
    <meta charset="utf-8" />
    <title>Edit</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
</head>

推荐答案

RenderSection 只能存在于布局文件(即母版页)中...它的目的是让您可以直接请求的页面以布局的各个部分为目标(布局是所有选择使用它的页面通用的文件)并为这些不同的部分提供内容.

RenderSection can only exist in Layout files (i.e. master pages)... its purpose is to allow the pages you can request directly to target various sections of a Layout (layout being a file common to all pages which choose to use it) and supply content for these different sections.

如果您想将此部分分离为可在许多页面上重用的内容,您应该将其放在部分中并将 rendersection 调用替换为类似的内容

If you want to separate this section out as something which is resuable on many pages you should put it in a partial and replace the rendersection call to something like

@Html.Partial("Scripts")

这篇关于文件“~/Views/Position/Edit.cshtml"不能直接请求,因为它调用了“RenderSection"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆