I have a form on my web page in which I need that when a user enters a Code in an input in another input, the Name is auto-completed. I have the code shown below, but it does nothing. I have two tables: TablaCodigos: Codigo_p varchar ...
TablePersonas Codigo_p varchar Name varchar ...
my script
<script type="text/javascript">
$("#Codigo_p").change(function () {
$.getJSON("/Solicitud/GetPersona?Codigo_p=" + $("#Codigo_p").val(), function (result)
{
var select = $("#nombreP");
select.empty();
$.each(result, function (index, itemData) {
select.append($({
value: itemData.Nombre
}));
});
});
}
</script>
my controller
public JsonResult GetPersona(string Codigo_p)
{
bd.Configuration.ProxyCreationEnabled = false;
TablaPersonas nom_p = bd.TablaPersonas.Where(x => x.Codigo_p == Codigo_p).Single();
return Json(nom_p, JsonRequestBehavior.AllowGet);
}
HTML
<div class="form-group">
<div class="m-0 font-weight-bold text-primary subtitulo">Persona:</div>
<input type="text" class="form-control" id="nombreP" name="nombreP" placeholder="Nombre" readonly="" >
</div>
You don't need a loop to iterate through the results because the response is just an object. To set the value you can set the input value equal to the property you want like so:
Or to simplify your code, the following line is equivalent:
From input I see that when select you empty it before filling the other input
What I would do is something like this
I hope it works for you!