ASP.NET MVC 编辑器模板 javascript 位置

发布于 2024-09-25 20:02:16 字数 325 浏览 6 评论 0原文

我们有一个编辑器模板,其中包含大约 40 行 jquery。我尝试将此脚本放入 块中,以将所有 javascript 保留在页面内的一个位置。但是,我收到以下错误消息内容控件必须是内容页中的顶级控件

有什么方法可以让它工作,这样我们就不会在最终输出页面周围添加脚本,或者有人可以推荐存储 ASP.NET MVC 模板中使用的 javascript 的最佳实践吗?目前,我正在考虑将代码提取到一个单独的文件中,并在母版页中引用它,但这意味着它会被提取到每个页面中,这并不理想。

提前致谢。

We have an editor template that contains approx 40 lines of jquery. I tried dropping this script into a <asp:Content> block to keep all of the javascript in one location within the page. However, I get the following error message content controls have to be top-level controls in a content page.

Is there any way to get this working so we don't have script dotted around our final output pages or could someone recommend the best practice for storing javascript used within ASP.NET MVC templates? At the moment I'm thinking of pulling the code into a separate file and referencing it within the master page but this means it gets pulled into every page which isn't really ideal.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-10-02 20:02:16

如果将 javascript 保存在单独的文件中并在需要的地方引用它,那么以后的维护会更容易。另外,如果您认为将所有脚本放入一个文件中会增加不必要的脚本加载(在不需要的地方),那么根据有用的功能/模块等策略将脚本分成单独的文件,然后引用它们。

另外,据说始终将脚本保留/引用在页面底部,以便页面加载速度更快。

It would be easier for later maintenance, if you keep the javascript into a separate file and reference it where ever it is needed. Also, if you feel that placing all script into a single file will increase unnecessary loading of scripts, where not needed, then break the scripts into separate files based on functionality/modules in which it is useful etc. strategy and then reference them instead.

Also, it was said that always, keep/reference the scripts at the bottom of the page so that page loading will be faster.

无所谓啦 2024-10-02 20:02:16

正如西瓦所说,页面底部是“理想的”。但是,至于单独的文件。只有当您不从页面引用 asp.net 元素时,这才实用 - 即:

<asp:Content ContentPlaceHolderID="jsCode" ID="jsCode1" runat="server">
    <script type="text/javascript">
        $(document).ready(function() {
            getPoundsData();
        });

        function getPoundsData() {
            var id = $("#ID").val();
            var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';
            var params = { id: id };
            if (id != null)
                SendAjaxCache("#" + $("#ShareholderID option:selected").text() + " RSP#", URL, params, null, ListDataShareholderPounds);
        }

        function ListDataShareholderPounds(data) {
            if (data.length != 0) {
                $('#shareholderPounds').html("");
                $('#shareholderPounds').show();
                $('#shareholderPounds').html(data);
            }
        };

    </script>
</asp:Content>

注意

var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';

js 中的:部分。 “我们”所做的就是在母版页的最底部添加一个内容部分来保存我们的 js 内容。但是,这只适用于 ViewPage (aspx) 对象。 ascx 页面“不知道”任何母版页内容部分。

我们目前正在致力于系统化该过程,通过该过程,我们保存“部分”js 文件,其中包含 asp.net 引用,然后通过过滤器属性将它们注入到页面流中。我们还处于早期阶段,但这种方法的好处是,部分 js 被视为文件,因此会被缓存以供将来访问该页面。

无论如何,这就是我们当前的方法,我们有兴趣发现窥视者是否使用任何类似的机制来注入包含 asp.net 对象引用的 js。

干杯...

[编辑] - 这是我正在谈论的方法的提示(这不是我们最初的灵感,但非常相似,不过是 Webforms,而不是 MVC) - http://www.west-wind.com/WebLog/posts/252178。 aspx 或这个 mvc: http://poundingcode.blogspot.com/2009/12/injecting-javasript-into-aspnetmvc-with.html
终于找到了激发我们“搜索”的文章: ASP .NET MVC 路由和路径是 js 文件 加上 http://codepaste.net/p2s3po

as siva says, bottom of the page is the 'ideal'. however, as for seperate files. this is only going to be practical as long as you don't reference asp.net elements from the page - ie:

<asp:Content ContentPlaceHolderID="jsCode" ID="jsCode1" runat="server">
    <script type="text/javascript">
        $(document).ready(function() {
            getPoundsData();
        });

        function getPoundsData() {
            var id = $("#ID").val();
            var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';
            var params = { id: id };
            if (id != null)
                SendAjaxCache("#" + $("#ShareholderID option:selected").text() + " RSP#", URL, params, null, ListDataShareholderPounds);
        }

        function ListDataShareholderPounds(data) {
            if (data.length != 0) {
                $('#shareholderPounds').html("");
                $('#shareholderPounds').show();
                $('#shareholderPounds').html(data);
            }
        };

    </script>
</asp:Content>

notice the:

var URL = '<%=Url.Action("GetPounds", "FundShareholder")%>';

part in the js. what 'we' do is to add a content section to the master page at the very bottom to hold our js stuff. however, this only works inside the ViewPage (aspx) object. the ascx pages are 'ignorant' of any master page content sections.

We are currently working on systemizing the process whereby we save 'partial' js files with asp.net references inside them and then inject them into the page-flow via a filterattribute. we're at an early stage with this but the nice thing about this approach is that the partial js is treated as a file and is therefore cached for future visits to that page.

anyway, that's our current approach, would be interested to discover if peeps are using any similar mechanisms to inject js that contains asp.net object references.

cheers...

[edit] - here's a heads up on the approach i'm talking about (this wasn't our original inspiration, but is quite similar, tho is webforms, rather than mvc) - http://www.west-wind.com/WebLog/posts/252178.aspx or this one which IS mvc: http://poundingcode.blogspot.com/2009/12/injecting-javasript-into-aspnetmvc-with.html.
Finally found the article that inspired our 'search' in this: ASP.NET MVC routing and paths is js files plus http://codepaste.net/p2s3po

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文