Javascript 如何在Ajax.beginfom中传递变量

Javascript 如何在Ajax.beginfom中传递变量,javascript,c#,asp.net,asp.net-mvc,Javascript,C#,Asp.net,Asp.net Mvc,您好,我是C#新手,我需要在js中的以下函数(PostFilure)中使用来自C#的字符串变量。问题是函数返回了一些对象,我不知道从哪里来。我的代码 Index.cshtml @using (Ajax.BeginForm("Login", "Account", new AjaxOptions { HttpMethod = "post", OnBegin = "PostOnBegin", OnFailure = "PostFailure", OnSuccess = "PostSu

您好,我是C#新手,我需要在js中的以下函数(PostFilure)中使用来自C#的字符串变量。问题是函数返回了一些对象,我不知道从哪里来。我的代码

Index.cshtml

 @using (Ajax.BeginForm("Login", "Account", new AjaxOptions {
    HttpMethod = "post", OnBegin = "PostOnBegin", OnFailure =
    "PostFailure", OnSuccess = "PostSuccess", OnComplete =
    "PostOnComplete" })) {...
anylib.js

 function PostFailure(message){

 }
其想法是:

Index.cshtml

 @using (Ajax.BeginForm("Login", "Account", new
 AjaxOptions { HttpMethod = "post", OnBegin = "PostOnBegin", OnFailure
 = "PostFailure(messaje,'hello')", OnSuccess
 = "PostSuccess", OnComplete = "PostOnComplete" }))
anylib.js

function PostFailure(message,x){

 }

您可以在
OnSuccess
事件处理程序方法中执行此操作

 function PostSuccess(ajaxContext) {
     //ajaxContext is the json representation of myModel returned from the server in Json(myModel);
     console.log(ajaxContext.MyReturnField1);
 }
OnFailure
处理程序将针对除200 ok http状态之外的任何其他状态启动。正常值为statusText,与“服务器上发生内部错误”类似,为500。但是,请确保您没有收到非200 http代码的专用对象,在这种情况下,xhr结果数据可能是任何内容

function PostFailure(exception) {
     console.log(exception);
     console.log(exception.statusText);
     console.log(exception.MyUserFiendlyErrorMessage);
     $("#divError").html(exception.ClientExceptionPartiaViewHTML);   
}

谢谢,但是如何将Index.cshtml中的变量字符串添加到函数PostFailure(异常,'hello'){}?您不能向事件添加参数。该处理程序正在解释通过ajax调用返回的内容。您可以将数据添加到返回内容的有效负载中。您想在ajax调用提交到服务器之前添加一个变量,还是在调用失败时从服务器返回一个变量?