Error using Ajax.BeginForm when giving a callback parameter for OnSuccess, OnFailure, OnComplete and OnBegin

℡╲_俬逩灬. 提交于 2019-12-11 17:41:36

问题


I'm trying to use an Ajax form, and whenever I provide a callback method to any of the AjaxOptions properties I get this error..

Microsoft JScript runtime error: Object doesn't support this property or method

If I do not provide a callback function to any of the properties (OnSuccess, OnFailure, OnComplete, or OnBegin) no error occurs and the form gets posted correctly.

Here is my code.

<% using (Ajax.BeginForm("someAction", "SomeController", new AjaxOptions() {
        HttpMethod="POST",
        UpdateTargetId = "feedbackMsg",
        OnSuccess = "ShowConfirmationMessage"
    }))
{ %>

My javascript code is this.

function ShowConfirmationMessage(xhr) { alert(xhr);}

When I provide the OnSuccess method the code gets rendered like this.

<form action="/Review/SaveQualityScore" 
    method="post" 
    onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));" 
    onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), 
        { 
            insertionMode: Sys.Mvc.InsertionMode.replace, 
            httpMethod: &#39;POST&#39;, 
            updateTargetId: &#39;feedbackMsg&#39;, 
            onSuccess: Function.createDelegate(this, ShowConfirmationMessage) 
        });"
>

I'm guessing the Function.createDelegate is the problem. Because when I remove the OnSuccess property the function.CreateDelegate doesn't get rendered and everything works fine.

Anyways, I'm going crazy over here, any help would be appreciated. Thank you!


回答1:


Make sure the following scripts are both included:

  • MicrosoftAjax.js
  • MicrosoftMvcAjax.js



回答2:


Or better yet...if you're using MVC 3, be sure to include jquery.unobtrusive-ajax.js. It'll render out your form tag cleaner.

Also, you might not want to include UpdateTargetId and OnSuccess together. I just spent 3 hours figuring out that if my controller is returning json, my onsuccess javascript callback function is never hit if I define UpdateTargetId in the Ajax.BeginForm.



来源:https://stackoverflow.com/questions/6347784/error-using-ajax-beginform-when-giving-a-callback-parameter-for-onsuccess-onfai

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!