1
I am developing an application in Asp.Net MVC and I am having a problem that I am not able to solve. I have a form with 2 buttons submit
, and when I do the submit
I want to get the name of the button I clicked on id
and thus alter 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’t get the name of this button to change the value of input text
. Soon after the form is submitted, I try to send again and finally I can get the id
and change the value of the input text
.
My question is: When I do the first Submit, it sends the whole form and does not take the id of the button and soh I can do it after processing ?
I’m trying to do it like 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"/>
}
You "can’t" have 2 Submit buttons in a form, power can, but the specification is that a form has a Submit button which is the button that sends the form. I believe you need to replace them with an input of type "button", and after capturing their click and picking up the ids, perform the form Ubmit
$(form).submit()
– gabrieloliveira
@gabrieloliveira managed to settle with the 2 Ubmit. Very simple incidentally, I removed the
$(document).ready(function(){})
and it worked. Thank you. !– FernandoPaiva
@Fernandopaiva the events
Click
are inside the$(document).ready()
– Renan Carlos