MVC + RegisterClientScriptInclude /的RegisterClientScriptBlock [英] MVC + RegisterClientScriptInclude / RegisterClientScriptBlock

查看:154
本文介绍了MVC + RegisterClientScriptInclude /的RegisterClientScriptBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何MVC相当于Page.ClientScripts.RegisterClientScriptInclude和Page.ClientScripts.RegisterClientScriptBlock?

我期待创造出可以很好地从相同的主视图中多次引用部分景色。

这些观点很可能会拥有自己的剧本的要求,但它似乎浪费编写包括每一个潜在的主视图或母版页上。

此外,我觉得这将是sensibel有局部视图调用JavaScript一次初始化它的所有兄弟姐妹的意见。

例如...

  $('输入[ALT =日期]')日期选择器()。

...的东西,我真的应该只需要调用一次,但只有部分视图已经把这样的HMTL控制在视图中。

所以任何想法我怎么能实现ASP.Net MVC框架内的老Page.ClientScripts.RegisterClientScriptInclude和Page.ClientScripts.RegisterClientScriptBlock功能


解决方案

您可以使用星火视图引擎,具有用于此目的曾经属性。例如。

 <内容NAME =MasterPageHead>
  <脚本一次=CustomScriptUniqueIdName... />
< /内容>

当在视图或部分,脚本中使用(或可以是任何其他标​​记,例如格)才会被称为MasterPageHead母版页部分包括一次(你可以将上述&lt; HEAD&GT;)<。 / p>

不知道这将是对你有帮助,但你问一个MVC解决方案,而不是WebForms的解决方案。

Is there any MVC equivalent to Page.ClientScripts.RegisterClientScriptInclude and Page.ClientScripts.RegisterClientScriptBlock?

I am looking to create Partial Views which may very well be referenced multiple times from the same main view.

These views might very well have their own script requirements but it seems wasteful to write includes on every potential main view or master page.

Also I feel that it would be sensibel to have the Partial view call some javascript once to initialise all of it's sibling views.

For example...

$('input[alt=date]').datepicker();

...is something which I should really only have to call once, but only if a partial view has placed such a hmtl control in the view.

So any ideas how I can achieve the old Page.ClientScripts.RegisterClientScriptInclude and Page.ClientScripts.RegisterClientScriptBlock functionality within the ASP.Net MVC Framework

解决方案

You can use Spark view engine, that has "once" attribute for this purpose. E.g.

<content name="MasterPageHead">
  <script once="CustomScriptUniqueIdName" ... />
</content>

When used in a view or a partial, script (or this can be any other tag, e.g. div) will only be included once in the master page section called MasterPageHead (which you can place in <head>).

Not sure if this will be helpful for you, but you asked for a MVC solution, not WebForms solution.

这篇关于MVC + RegisterClientScriptInclude /的RegisterClientScriptBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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