且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MVC + RegisterClientScriptInclude /的RegisterClientScriptBlock

更新时间:2023-12-05 23:33:52

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

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

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

不知道这将是对你有帮助,但你问一个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.