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 ;)