Getting return value from action with jquery.forms.js options...MVC3

Im including the popular file jquery.form.js. I have the following:

 var options = {
    beforeSubmit: beforeSubmit,  // pre-submit callback 
    success: afterSubmit  // post-submit callback 
  };

 $('#myForm').submit(function (e) {
     $(this).ajaxSubmit(options);
     return false;
  });


 function afterSubmit(responseText, statusText, xhr, $form) {
   // i want to check for an error
}

I call this action:

    [HttpPost] 
    public string UploadDocument(DocumentModel model)
    {
        if (noerror)
        return "ok";
        else
         return "the error";
    }

Somewhere in the parameters 'responseText, statusText, xhr, $form' the return string is stored.

Where is my return string stored, or how do i store it, so i can check the results when the 'afterSubmit' javascript function is called? Thanks

Answers


Never return strings from controller actions. In ASP.NET MVC controller actions should return ActionResults:

[HttpPost] 
public string UploadDocument(DocumentModel model)
{
    if (noerror)
        return Content("ok");
    else
        return Content("the error");
}

and then the result will be stored in the responseText variable.

Obviously testing in your javascript if (responseText == 'ok') seems like something absolutely horrible and for this reason there's JSON:

[HttpPost] 
public string UploadDocument(DocumentModel model)
{
    if (noerror)
        return Json(new { success = true });
    else
        return Json(new { success = false });
}

so that in your javascript you could work directly with the underlying types (boolean in this case):

function afterSubmit(response, statusText, xhr, $form) {
    if (response.success) {
        alert('super! we succeeded');
    } else {
        alert('Oh snap!');
    }
}

Need Your Help

C# CopyFromScreen filling up memory

c# .net memory screenshot

I am using this code to take screen shot every second with timer, but it seems to be filling up my memory (about 30 MB each take)... This is the code: