如何在Ajax.BeginFrom中传递变量

时间:2016-02-02 14:56:35

标签: 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 = "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){

 }

1 个答案:

答案 0 :(得分:0)

您可以在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);   
}