Finding Data with jQuery

2

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);
    }
    
asked by anonymous 05.08.2015 / 21:26

1 answer

1

This is wrong here:

$.getJSON("/Cursos/RetornaAluno" + cpf + "&formato=json", {}, function (data) { ... });

Possibly the default route does not accept CPF as an argument, so you need to specify the parameter name in the URL. That is:

$.getJSON("/Cursos/RetornaAluno/?cpf=" + cpf, {}, function (data) { ... });

About the Controller , some things caught my attention:

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);
}

This:

var aluno = from a in db.Alunos select a;
aluno = aluno.Where(a => a.cpfAluno == cpf);

Can be replaced by this:

var aluno = db.Alunos.FirstOrDefault(a => a.cpfAluno == cpf);

The first reason is performance. Here:

var aluno = from a in db.Alunos select a;

You are bringing all students into the application memory to select only one record. Nothing good in performance.

The second reason is simplicity.

Possibly this here is wrong too:

        if (data.aluno_txt != null) {
            $("#nome").val(data.nome);
            $("#idAluno").val(data.idAluno);
        }
        else {
            alert("Aluno não encontrado");
            $("#cpf").focus();
        }

If the student is not found, data is empty. Therefore, test if% of integer% proceeds as follows:

        if (data) {
            $("#nome").val(data.nome);
            $("#idAluno").val(data.idAluno);
        }
        else {
            alert("Aluno não encontrado");
            $("#cpf").focus();
        }

This should work.

    
05.08.2015 / 22:37