You can do this as follows
View:
<form id="myForm">
<input type="text" name="Barconteudo" id="Barconteudo" />
<input type="text" name="Teste" id="Teste" />
<input type="button" btn="myBtn" value="Enviar" onclick="enviaInformacoes()" />
</form>
<script>
function enviaInformacoes() {
var objeto = $("#myForm").serialize();
$.ajax({
url: "/BarCode/BarGenerate",
type: "POST",
data: objeto,
datatype: "json",
success: function (data) {
alert(data.Mensagem);
}
});
}
</script>
Controller:
public ActionResult BarGenerate(QRCodeModel model)
{
return Json(new { Mensagem = $"{model.Barconteudo} - {model.Teste}" });
}
Model:
public class QRCodeModel
{
public string Barconteudo { get; set; }
public string Teste { get; set; }
}
Explanation:
We have two inputs
one with name
and ID
Barconteudo other Test and a button that when clicked calls the function enviaInformacoes()
.
In the function, with JQuery
a serialize
of form
is made that causes all fields to be "played" in an object (it will create an object with the Barconteudo and Test fields), after that, with % with% is indicated with $.ajax
, type (Post or Get), data to be sent, type (json) and if successful in the request, an alert is issued with the field "Message".
No URL
has been changed to receive an object of type QRCodeModel containing the Barconteudo and Test properties. in addition to the return type that was changed to controller
with the "Message" field