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

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

问题描述

我想所有的,我可以在部分重复使用的东西分开,所以这将是我更容易维护。

不过,我得到这个异​​常:
文件〜/查看/位置/ Edit.cshtml不能直接要求,因为它所谓的RenderSection的方法

我创建了一个包含以下内容sections.cshtml文件名为:

  @section脚本{
    <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
    <脚本的src =@ Url.Content(〜/脚本/ jquery.validate.unobtrusive.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
}

而在_layout.cshtml文件我把它改为:

 < HEAD>
    <间的charset =UTF-8/>
    <标题> @ ViewBag.Title< /标题>
    <链接HREF =@ Url.Content(〜/内容/的site.css)的rel =stylesheet属性类型=文/ CSS/>
    @RenderSection(脚本,必需:false)
    @ *<脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.5.1.min.js)TYPE =文/ JavaScript的>< / SCRIPT>
    <脚本的src =@ Url.Content(〜/脚本/ Modernizr的-1.7.min.js)TYPE =文/ JavaScript的>< / SCRIPT> * @
< /头>

当我去在浏览器中查看,并检查源$ C ​​$ C它只显示:

 < HEAD>
    <间的charset =UTF-8/>
    <标题>编辑< /标题>
    <链接HREF =/内容/的site.css的rel =stylesheet属性类型=文/ CSS/>
< /头>


解决方案

RenderSection只能在布局文件存在(即母版页)......其目的是为了让你可以直接要求来定位布局的各个部分页面(布局是一个文件,共同到选择使用它的所有页面),并为这些不同的部分所提供的内容。

如果你想分开这部分出来的东西是可重复使用的许多页面上,你应该把它放在一个部分和替换像

的rendersection调用的东西

  @ Html.Partial(脚本)

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

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

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>
}

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 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.

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")

这篇关于文件&QUOT;〜/查看/位置/ Edit.cshtml&QUOT;不能直接请求,因为它会调用&QUOT; RenderSection&QUOT;方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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