前布局code MVC执行视图code和毁了我的脚本命令 [英] MVC executing view code before layout code and ruining my script order

查看:152
本文介绍了前布局code MVC执行视图code和毁了我的脚本命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将所有我的javascript包括在页面的底部。我使用MVC剃刀。我写了一个辅助方法来注册脚本。它保留在他们注册的顺序执行这些脚本,它排除了愚弄。

I'm trying to move all of my javascript includes to the bottom of the page. I'm using MVC with Razor. I've written a helper method to register scripts. It keeps the scripts in the order they're registered and it excludes dupes.

@{ Html.RegisterScript("/scripts/someFile.js"); }

然后右边的结束标记之前我做这样的事情:

And then right before the closing body tag I do something like this:

    @Html.RenderScripts()
</body>

如果该脚本注册的所有在一个页面上这一切工作正常。如果我注册在我的布局一些脚本,并在我的内心认为一些脚本,事情出差错。基本上,内部视图布局之前执行。因此,事件虽然在内部视图中的脚本依赖于外部视图中的脚本,他们正在注册之前

This all works fine if the scripts are registered all on one page. If I register some scripts in my layout, and some scripts in my inner view, things go awry. Basically, the inner view is executing before the layout. So event though the scripts in the inner view depend on the scripts in the outer view, they're being registered prior.

所以,如果我有_Master.cshstml,它有

So if I have _Master.cshstml and it has

@{
   Html.RegisterScript("script1.js");
   Html.RegisterScript("script2.js");
}

然后我有InnerView.cshtml与_Master.cshtml作为其布局和我的看法

And then I have InnerView.cshtml with _Master.cshtml as its layout and in the view I have

@{
   Html.RegisterScript("script3.js");
}

脚本按照这个顺序得到呈现出来:script3,SCRIPT1,脚本2

The scripts get rendered out in this order: script3, script1, script 2.

有没有什么办法可以强制布局内视图前执行?我试着搬东西遍布内部景观似乎总是第一个来执行的地方和一切。也许这是正常吗?如果是这样,什么是我要完成我想要做的最好方法是什么?

Is there any way I can force the layout to execute before the inner view? I've tried moving things all over the place and everything the inner view always seems to execute first. Perhaps this is expected? If so, what is the best way for me to accomplish what I'm trying to do?

推荐答案

我最终通过跟踪每个视图脚本的解决这个问题。基本上我保持一个

I ended up solving this problem by keeping track of the scripts per view. Basically I keep a

Dictionary<string, SortedList<int, string>> registeredScripts;

 SortedList<int, string> viewOrder;

我每次注册一个剧本我通过在脚本路径和视图名称(从ViewDataContainer.ToString检索())。我跟踪中,我已经看到了viewOrder对象的意见秩序。然后,我用字典来跟踪每个视图的脚本。最关键的是视图名称。当谈到时间来呈现出的一切,我扭转viewOrder SortedList的&LT;>谱写出由视图中的所有脚本。尼斯位,这些脚本是为了通过视图,并一度扭转了看法顺序是正确的。

Every time I register a script I pass in the script path and the view name (retrieved from ViewDataContainer.ToString()). I keep track of the order in which I've seen the views in the viewOrder object. I then use the dictionary to keep track of the scripts per view. The key is the view name. When it comes time to render everything out, I reverse the viewOrder SortedList<> and write out all the scripts by view. The nice bit is, the scripts are in order by view, and the view order once reversed is correct.

我做了一个可怕的工作解释说,但由于版权问题,我不能共享code。

I did a horrible job explaining that, but for copyright reasons I cannot share the code.

这篇关于前布局code MVC执行视图code和毁了我的脚本命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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