开发者

MVC 3 Razor - Use script references in Layout page from Partial View

I used some jquery in my partial view 开发者_开发知识库and realised that I need to have the script reference put in the partial view Again in order for the jquery to works.

I wonder is there any method which I can "call" all the script references in the Layout page so I dont need to duplicate it in the Partial View??

I tried to search for related information and tried to use @section to store the script references, but it seems cant work.

Hope can get some guide here... Appreciate it...


You can put all your script references in a Partial View of its own, and then call @Html.Partial("ScriptReferences") from both the Layout page and the other Partial View that needs the script references.


I normally include/reference all the scripts in my layout view. Although some are not needed on all pages.

The partial views, do not have to worry about a script not available. And it also saves time during development.

The overhead of having all scripts available is minimal. Specially if you merge all .js files in one big minimized script (Chirpy can do that for you). Modern browsers will know it's the same script and not download and parse them over and over.


Script references in Layout page are automatically shared with partial views. You might encounter issues if the Home page (which loads layout) URL and the partial view URLs are not relative ... Use relative URLs to load the partial views and the sharing of script file references and global namespaces would work ...


I found another option that turned out to make my life a lot better: http://kazimanzurrashid.com/posts/asp-dot-net-mvc3-razor-script-loading-tips

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜