asp.net-mvc 将 Javascript 函数分配给 AjaxOptions OnSuccess 属性引发错误 - ASP.NET MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/695729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:27:06  来源:igfitidea点击:

Assign a Javascript function to AjaxOptions OnSuccess property raise an error - ASP.NET MVC

asp.net-mvcasp.net-ajax

提问by segaco

I'm using the Ajax.ActionLink helper to generate a link to delete a record. This is the code:

我正在使用 Ajax.ActionLink 帮助程序生成删除记录的链接。这是代码:

Ajax.ActionLink("Delete Image", "DeleteImage", new { id = item.Id },
                                               new AjaxOptions { HttpMethod = "Delete", OnSuccess = "Test()" } )

I'm assign a Javascript function (Test()) to the OnSucess property because I want to do some JQuery stuff, but when I click the Delete link this error message is raised

我将一个 Javascript 函数 (Test()) 分配给 OnSucess 属性,因为我想做一些 JQuery 的事情,但是当我单击删除链接时,会出现此错误消息

Microsoft JScript runtime error: 'b' is null or not an object

Microsoft JScript 运行时错误:“b”为空或不是对象

in the MicrosoftAjax.js file (Line 5, Column 62099). If I remove the OnSuccess property, everything works fine (even if the Test() function is empty, the same error is raised). Thanks for your help!

在 MicrosoftAjax.js 文件中(第 5 行,第 62099 列)。如果我删除 OnSuccess 属性,一切正常(即使 Test() 函数为空,也会引发相同的错误)。谢谢你的帮助!

回答by silverfighter

OnSuccess = "Test()" 

you have to write it like this it is a callback...

你必须像这样写它是一个回调......

OnSuccess = "Test"

回答by atzu

If you have to pass any parameter to the OnSuccess event you may have to write the funcion in this way.

如果您必须将任何参数传递给 OnSuccess 事件,您可能必须以这种方式编写函数。

OnSuccess = "function(){exampleFunction('" + param1 + "');}"

回答by Sam Peacey

To pass a parameter, an anonymous function won't work, you need to do something like this:

要传递参数,匿名函数将不起作用,您需要执行以下操作:

OnSuccess = String.Format("Test({0})", param)