0

As the title says I have a problem with a save button which saves many times. Sometimes it works as it should and sometimes saves 2,3,5,13 times. The number is different everytime.

Some parts of the code will be hidden for copyright reasons. Sorry ;(

At first I thought it could be my mouse clicking more than once on the button so I added the following code to the jquery that calls the method in the Controller:

@(vm).saveobject= function() {
     $("#savebutton").attr('disabled', 'disabled');
     var url = //Some valid Url      

     var entity = {//code here
     };

     $.ajax({
         url: url,
         type: 'post',
         contentType: 'application/json',
         dataType: 'json',
         data: JSON.stringify(entity),
         success: function (resultado) {
             var result = resultado.result.toString();
             if (result.toLowerCase() == "true".toLowerCase()) {
                 $('#Grid').data("tGrid").rebind()
                 alert("Blablabla");
             } else {
                 alert(resultado.error);
             }
         }
     });
     $("#saveButton").removeAttr('disabled');
 };

Code of the controller:

    [HttpPost]
    public virtual JsonResult Save(Onject o)
    {
        string error = string.Empty;
        if (//Conditions)
        {
            Repository.SaveOrUpdate(o);
            return Json(new { result = true ,error = error}, JsonRequestBehavior.AllowGet);
        }
        else {
            error = "Blablablabla";
            return Json(new { result = false, error = error }, JsonRequestBehavior.AllowGet);
        }
    }

But that didn't work. I read the following question How do I prevent multiple form submission in .NET MVC without using Javascript? but I don't know if might be helpful as the method already implements [HttpPost]

Do you have any idea?

Thanks in advance ;)

Community
  • 1
  • 1
WristMan
  • 337
  • 3
  • 11
  • Move the `$("#saveButton").removeAttr('disabled');` statement in the `success` or better in `complete` callback of ajax. – Tushar Nov 10 '15 at 16:19
  • Are you sure that it makes multiple ajax calls ? Did you open the browser console/network tab to confirm this ? Or is it a problem with your Repository.SaveOrUpdate method ? – Shyju Nov 10 '15 at 16:20
  • @Shyju Hi. I am sure it calls the method more than once. – WristMan Nov 10 '15 at 16:27

0 Answers0