Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Html助手在页面末尾插入脚本_C#_Asp.net Mvc 4_Html Helper - Fatal编程技术网

C# 使用Html助手在页面末尾插入脚本

C# 使用Html助手在页面末尾插入脚本,c#,asp.net-mvc-4,html-helper,C#,Asp.net Mvc 4,Html Helper,我有一个生成简单Html表的Html助手。我想在同一个助手中生成一些脚本来处理这个表,比如编辑行、重新排序等 我已经有了这些脚本,我的问题是: 如何通过HTML助手生成这些脚本并将其放在页面末尾? 我目前在html表的末尾添加了脚本,比如一个字符串(表和脚本的html)并将其打印在页面上,有没有更好的方法,解决这个问题的最佳实践 为了让问题更清楚,这是我的助手签名: public static MvcHtmlString CustomTableRepository (this Ht

我有一个生成简单Html表的Html助手。我想在同一个助手中生成一些脚本来处理这个表,比如编辑行、重新排序等

我已经有了这些脚本,我的问题是:

如何通过HTML助手生成这些脚本并将其放在页面末尾?

我目前在html表的末尾添加了脚本,比如一个字符串(表和脚本的html)并将其打印在页面上,有没有更好的方法,解决这个问题的最佳实践

为了让问题更清楚,这是我的助手签名:

public static MvcHtmlString CustomTableRepository
       (this HtmlHelper html, string idTable, Hashtable columns, string jsonRepo)
其中:

idTable: Id of the tag table
columns: The key/value of each th cell of the columns
jsonRepo: jSon repository, where I will take the values of my table and draw it

这听起来并不特别糟糕,唯一的缺点是 1.它不会缩小 2.如果有多个表,则有重复的脚本 3.它不会被缓存,因此每次加载页面时都会被发送

编辑:新建议

创建列表并将其添加到viewbag。在html帮助程序中,将需要的脚本添加到此列表中。在布局结束时,使用linq选择唯一的脚本名称,并进行渲染

老建议-如果你想读,就读吧。它包含好的实践

如果该表用于大多数页面,只需将其放在脚本文件夹中并包含在布局中即可

如果它只在几个页面上使用,那么您需要将它保存在帮助器中。您始终可以将脚本移动到脚本文件夹中,如脚本>元素>表.js。这也许有点道理。然后通过函数将其包含在助手中。就像使用jquery一样,您可以编写文本

<script> $(function(){ $.getScript("Scripts/Element/Table.js") }); </script>
$(function(){$.getScript(“Scripts/Element/Table.js”)});