I have this method on the Controller
[HttpPost]
public JsonResult PreencheEndereco(string _cpf)
{
AgaxturCmsEntities db = new AgaxturCmsEntities();
try
{
var Result = (from a in db.TB_CLIENTES
where a.CdCliente == "1" && a.CPF == _cpf
select new {
a.Endereco,
a.Numero,
a.CEP,
a.Complmento,
a.Telefone,
a.Celular
}).ToList();
return Json(new { Result }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(new { Result = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
What I need is to do a jquery function and fill in those 6 fields returned by the function. This is my role skeleton, but how do I now fill in?
$(function () {
$("#btnEndereco").click(function () {
$.ajax({
url: '/Passo/PreencheEndereco',
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
data: JSON.stringify({ _cpf: $("#CPF").val() }),
success: function (data) {
$(data.Result).each(function () {
$("#endereco").val(this.Endereco);
});
},
error: function (error) {
}
});
});
});
In this line that I think should enter the bank return in the field, does not work. I wonder if anything else is needed:
$("#endereco").val(this.Endereco);
These are the fields that need to be filled in, in HTML. I put an id for each of them, to get the jquery. The id's are for the inputs.
<div class="form-group">
<div class="grid_4">
<label>CEP</label>
</div>
<div class="grid_14">
<input id="cep" type="number" name="txtCep" class="grid_3 required" placeholder="00000-000" required />
</div>
</div>
<div class="form-group">
<div class="grid_4">
<label>Endereço</label>
</div>
<div class="grid_14">
<input id="logradouro" type="text" name="txtLogradouro" class="grid_14 required" placeholder="Nome completo" required />
</div>
</div>
<div class="form-group">
<div class="grid_4">
<label>Número</label>
</div>
<div class="grid_4">
<input id="numero" type="text" name="txtNumero" class="grid_3 required" required />
</div>
<div class="grid_2">
<label>Complemento</label>
</div>
<div class="grid_8">
<input id="complemento" type="text" name="txtComplemento" class="grid_8 required" required />
</div>
</div>
<div class="form-group">
<div class="grid_4">
<label>Telefone</label>
</div>
<div class="grid_6">
<input id="telefone" type="text" name="txtTelefone" class="grid_6 required" required />
</div>
<div class="grid_2">
<label>Celular</label>
</div>
<div class="grid_6">
<input id="celular" type="text" name="txtCelular" class="grid_6 required" required />
</div>
</div>