gpt4 book ai didi

asp.net-mvc-4 - 在部分 View 中使用部分

转载 作者:行者123 更新时间:2023-12-02 20:46:42 25 4
gpt4 key购买 nike

在我的共享布局中,我希望有一个“脚本”部分来填充页面功能所需的所有脚本。

布局.cshtml

<html>
<head>
<title>Test</title>
<script src="@Url.Content("~/Scripts/jquery-2.0.3.js")" type="text/javascript"> </script>

@RenderSection("Scripts", required: false)

</head>
<body>
@RenderBody()
</body>
</html>

所以,我的 View 加载了一个特定的 JavaScript,我希望它位于“脚本”部分,并且它正在工作。

Index.cshtml

@model PlatformaPu.Areas.Inventura.Models.Home.Index

@section Scripts {
<script src="@Url.Content("~/Areas/Inventura/Scripts/Home/Index.js")" type="text/javascript"></script>
}

{CONTENT REMOVED FOR BREVITY}

@section Footer {
@Html.Partial("~/Views/Shared/_AppSelector.cshtml", Model.AppSelector)
}

最后,我的 View 呈现了一个部分,并且我有一个可以加载该部分的 JavaScript。

_AppSelector.cshtml

@model PlatformaPu.Models.Shared._AppSelector

@section Scripts {
<script src="@Url.Content("~/Scripts/Shared/_AppSelector.js")" type="text/javascript"></script>
}

{CONTENT REMOVED FOR BREVITY}

...这工作 - javascript标签在“脚本”部分呈现

我该怎么做?

最佳答案

this question 中所述,不可能在局部 View 中使用部分:

Sections don't work in partial views and that's by design. You may use some custom helpers to achieve similar behavior, but honestly it's the view's responsibility to include the necessary scripts, not the partial's responsibility. I would recommend you using the @scripts section of the main view to do that and not have the partials worry about scripts.

您应该将脚本引用添加到引用部分的主视图。

关于asp.net-mvc-4 - 在部分 View 中使用部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18183462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com