代码之家  ›  专栏  ›  技术社区  ›  Mike U

页面上的渲染部分

  •  0
  • Mike U  · 技术社区  · 7 年前

    使用Razor页面@RenderSection在Razor页面上似乎不可用,我得到“RenderSection在当前上下文中不存在”

    如果我只是在部分页面上定义部分,则不会呈现任何内容,因此在使用部分的页面上进行一些搜索后,似乎需要在各个部分中添加RenderSection。

    任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   TanvirArjel    7 年前

    也许你没有抓住使用的要点 @RenderSection 在ASP.NET内核中。 应该只在 Layout

     <script src="~/lib/jquery/dist/jquery.js"></script>
     @RenderSection("Scripts", required: false)
    

    然后,页面应如下所示:

    @{
        ViewData["Title"] = "My Razor Page";
    
        Layout = "_Layout"; // If this specified in the `_ViewStart.cshtml` then you don't need it
    }
    
    <partial name="_YourPartial.cshtml"/>
    @section scripts {
        <script src="~/js/yourjs.js" asp-append-version="true"></script> // this is specific to this Razor page only and it will also be available on the partial view called inside this Razor Page
    }
    

    在生成html的过程中 scripts 在您的Razor页面上,将按如下方式呈现:

    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/js/yourjs.js" asp-append-version="true"></script>