If anyone can help me:
I'm doing an ASP.NET MVC system of academic control. At the time of enrollment, I wanted to put a blank field to enter the CPF, and other fields as the name, but only for presentation, and get the id to effect the enrollment.
Would do a search for the CPF in Controller and return the student or a message saying that the student was not found. I've tried some functions but no success.
I put more or less functions of this type:
<script>
$('#cpf').change(function (e) {
e.preventDefault();
$("#nome").val('');
$("#idAluno").val('');
var cpf = $('#cpf').val();
$.getJSON("/Cursos/RetornaAluno" + cpf + "&formato=json", {}, function (data) {
if (data.aluno_txt != null) {
$("#nome").val(data.nome);
$("#idAluno").val(data.idAluno);
}
else {
alert("Aluno não encontrado");
$("#cpf").focus();
}
});
});
</script>
And I created a ActionResult
in Controller that returns the object. I had never used jQuery so I do not know how to do it.
public ActionResult RetornaAluno(string cpf)
{
var aluno = from a in db.Alunos select a;
aluno = aluno.Where(a => a.cpfAluno == cpf);
return Json(aluno, JsonRequestBehavior.AllowGet);
}