I'm developing an application in Asp.Net MVC and am having a problem that I can not solve. I have a form with 2 buttons submit
, and when I do submit
I want to get the name of the button to which I clicked by id
and thus change a input text
of the form. The problem is that before getting the name of this button the form is all sent and I can not get the name of this button to change the value of input text
. Right after the form is submitted, I try to send again and finally I get the id
of the button and change the value of input text
.
My question is: Because when I submit the first submit, it sends the entire form and does not take the button id and only do it after processing?
I'm trying to do this.
Jquery
$(document).ready(function () {
$('#addPropriedade').submit(function (e) {
e.preventDefault();
$('#gravarPublicar').click(function () {
$('#status').val("1");
});
$('#gravar').click(function () {
$('#status').val("0");
});
var dados = new FormData($('#addPropriedade').get(0));
$("#errorMessage").hide();
var loading = $("#div_loading");
$(document).ajaxStart(function () {
loading.show();
});
$(document).ajaxStop(function () {
loading.hide();
});
$.ajax({
accepts: { json: 'application/json' },
dataType: 'json',
type: "POST",
url: "/Propriedade/addAjax",
data: dados,
processData: false,
contentType: false,
success: function (data) {
var status = data["status"];
var msg = data["msg"];
if (status === "1") {
$('#addPropriedade').trigger("reset");
$("#errorMessage").html(msg);
$("#errorMessage").prop("class", "alert-success");
//window.location.replace("/Empresa/view");
} else {
$("#errorMessage").html(msg);
$("#errorMessage").prop("class", "alert-danger");
}
$("#errorMessage").show()
},
error: function (xhr, status) {
$("#errorMessage").html("Erro tentando cadastrar");
$("#errorMessage").prop("class", "alert-danger");
$("#errorMessage").show()
}
});
return false;
});
});
Form
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "addPropriedade", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<!--Alterar o valor no submit -->
@Html.HiddenFor(model => model.status);
<input type="submit" class="btn btn-success" value="Gravar e Publicar" id="gravarPublicar" name="gravarPublicar" />
<input type="submit" class="btn btn-success" value="Gravar" id="gravar" name="gravar"/>
}