如何从 ASP.NET MVC 框架中的 AjaxOptions 获取生成的 JavaScript?
我正在尝试 ASP.NET MVC 框架,并想创建一个 ajax 帮助器方法。基本上,这个助手的行为类似于 ActionLink,但不对其链接文本进行编码。编写 HtmlHelper 相当简单,您只需编写自己的GenerateLinkInternal 版本即可。但这对于 AjaxHelpers 不起作用,因为GenerateLink 的ajax 版本间接调用内部的ToJavascriptString(通过GenerateAjaxScript),因此不能在MVC 程序集外部调用。我当然可以重写整个事情,但这似乎有点矫枉过正,有更好的方法吗?
最终,我想让这个助手像 BeginForm 一样运行,使链接包围 HTML 块。我还没有看过它,但我假设它也使用 ToJavascriptString 。我在网上搜索并浏览了 MVC 源代码,我开始怀疑我是否完全走错了路。
谢谢
更新:我越看这个问题,就越觉得根本没有解决方案。编写 MVC 框架的人并没有考虑过帮助人们编写自己的助手!
更新:我最终编写了一个几乎重复 AjaxOptions 功能的助手。
I'm trying out ASP.NET MVC Framework and would like to create an ajax helper method. Basically this helper would act like ActionLink but without encoding its link text. It is rather trivial to write as an HtmlHelper, you simply have to write your own version of GenerateLinkInternal. This isn't working for AjaxHelpers though, as the ajax version of GenerateLink is indirectly calling ToJavascriptString (through GenerateAjaxScript) which is internal, thus cannot be called outside the MVC assembly. I sure can rewrite the whole thing, but it seems way overkill, is there a better way?
Ultimately, I'd like to make this helper act like BeginForm to make the link surround a block of HTML. I've not looked at it yet, but I assume that it uses ToJavascriptString too. I've searched the web and, looking through the MVC source code, I begin to wonder if I'm completely on the wrong track.
Thanks
Update: The more I look at this problem, the more I think that there's simply no solution. Whoever wrote the MVC Framework didn't think about helping people write their own helpers!
Update: I've ended up writing an helper that pretty much duplicate AjaxOptions functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过从头开始编写自己的帮助程序来更轻松地做到这一点(即不要调用任何
Html.ActionLink()
/Ajax.ActionLink()
方法),只需使用Url.Action()
即可。例如,这样做非常简单:
您当然可以使用重载和额外参数来扩展它,以满足您自己的需求。
You could probably do this a lot easier by writing your own helper from scratch (i.e. don't make calls to any of the
Html.ActionLink()
/Ajax.ActionLink()
methods) simply by usingUrl.Action()
instead.For example, it's pretty trivial to do this:
You can of course extend this with overloads and extra parameters to suit your own needs.