Tengo un campo en donde el usuario al colocar su DNI se llenará automáticamente los otros campos. El problema es que no llega a traer los datos del usuario y no me muestra ningún error.
Clase Interesado
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace FormularioCharlas.Models
{
public class ClsInteresado
{
public string ID_Inter { get; set; }
public string dni_Inter { get; set; }
public string apePat_Inter { get; set; }
public string apeMat_Inter { get; set; }
public string nombres_Inter { get; set; }
public string fecha_nac_Inter { get; set; }
public string edad_Inter { get; set; }
public string sexo_Inter { get; set; }
public string correo_Inter { get; set; }
public string telefono_Inter { get; set; }
public string direccion_Inter { get; set; }
public string depar_Inter { get; set; }
public string provin_Inter { get; set; }
public string dist_Inter { get; set; }
}
}
Controlador
[HttpPost]
public JsonResult PostBackAjax(string campo_DNI_JSON)
{
ServicesReniec.sConsultaDNISoapClient consultaDNI = new ServicesReniec.sConsultaDNISoapClient();
List<string> resultado = new List<string>();
resultado = consultaDNI.Consulta("JVEGA", campo_DNI_JSON);
string ape_pat = "";
string ape_mat = "";
string nombres = "";
string fech_nac = "";
string edad = "";
string sexo = "";
string correo = "";
string telefono = "";
string direccion = "";
string departamento = "";
string provincia = "";
string distrito = "";
string ubigeo = "";
int i = 0;
foreach (string R in resultado)
{
switch (i)
{
case 0:
if ((R.Substring(0, 4) != "0000"))
{
//limpiarFormulario();
return Json(new ClsInteresado());
}
break;
case 1:
ape_pat = R;
break;
case 2:
ape_mat = R;
break;
case 4:
nombres = R;
break;
case 8:
if (R.Equals(" "))
{
departamento = "CALLAO";
}
else
{
departamento = R;
}
break;
case 9:
provincia = R;
break;
case 10:
distrito = R;
break;
case 11:
direccion = R;
break;
case 21:
fech_nac = R;
break;
}
i++;
}
ClsInteresado interesado = new ClsInteresado
{
apePat_Inter = ape_pat,
apeMat_Inter = ape_mat,
nombres_Inter = nombres,
fecha_nac_Inter = fech_nac,
edad_Inter = edad,
sexo_Inter = sexo,
correo_Inter = correo,
telefono_Inter = telefono,
direccion_Inter = direccion,
depar_Inter = departamento,
provin_Inter = provincia,
dist_Inter = distrito
};
return Json(interesado);
}
Vista Interesado
@model FormularioCharlas.Models.ClsInteresado
@{
ViewBag.Title = "CrearInteresado";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@Scripts.Render("~/bundles/jquery")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<h2>CrearInteresado</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>ClsInteresado</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ID_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ID_Inter, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ID_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.dni_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.dni_Inter, new { htmlAttributes = new { @id = "campo_dni", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.dni_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.apePat_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.apePat_Inter, new { htmlAttributes = new { @id = "campo_apePat", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.apePat_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.apeMat_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.apeMat_Inter, new { htmlAttributes = new { @id = "campo_apeMat", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.apeMat_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.nombres_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.nombres_Inter, new { htmlAttributes = new { @id = "campo_nombres", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.nombres_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.fecha_nac_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.fecha_nac_Inter, new { htmlAttributes = new { @id = "campo_fechNac", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.fecha_nac_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.edad_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.edad_Inter, new { htmlAttributes = new { @id = "campo_edad", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.edad_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sexo_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sexo_Inter, new { htmlAttributes = new { @id = "campo_sexo", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.sexo_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.correo_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.correo_Inter, new { htmlAttributes = new { @id = "campo_correo", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.correo_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.telefono_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.telefono_Inter, new { htmlAttributes = new { @id = "campo_telefono", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.telefono_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.direccion_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.direccion_Inter, new { htmlAttributes = new { @id = "campo_direccion", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.direccion_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.depar_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.depar_Inter, new { htmlAttributes = new { @id = "campo_departamento", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.depar_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.provin_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.provin_Inter, new { htmlAttributes = new { @id = "campo_provincia", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.provin_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.dist_Inter, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.dist_Inter, new { htmlAttributes = new { @id = "campo_distrito", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.dist_Inter, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script>
$(document).ready(function () {
let dni = $('#campo_dni');
let apePat = $('#campo_apePat');
let apeMat = $('#campo_apeMat');
let nombres = $('#campo_nombres');
let fechNac = $('#campo_fechNac');
let edad = $('#campo_edad');
let sexo = $('#campo_sexo');
let correo = $('#campo_correo');
let telefono = $('#campo_telefono');
let direccion = $('#campo_direccion');
let departamento = $('#campo_departamento');
let provincia = $('#campo_provincia');
let distrito = $('#campo_distrito');
function formPost() {
let dniValor = dni.val();
$.ajax({
type: "POST",
url: "/Charlas/PostBackAjax",
data: "{ campoDniJSON: 'dniValor'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (rpta) {
apePat.val(rpta.apePat_Inter);
apeMat.val(rpta.apeMat_Inter);
nombres.val(rpta.nombres_Inter);
fechNac.val(rpta.fecha_nac_Inter);
edad.val(rpta.edad_Inter);
sexo.val(rpta.sexo_Inter);
correo.val(rpta.correo_Inter);
telefono.val(rpta.telefono_Inter);
direccion.val(rpta.direccion_Inter);
departamento.val(rpta.depar_Inter);
provincia.val(rpta.provin_Inter);
distrito.val(rpta.dist_Inter);
}
});
}
$('#campo_dni').focusout(function () {
formPost();
});
});
</script>