Search Data with jQuery

Asked

Viewed 195 times

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 registration.

I would do a search for the number on Controller and returns the student or a message saying that the student was not found. I have tried some functions but without 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 the Controller that returns the object. I had never used jQuery so I don’t know how to do.

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

1 answer

1

This is wrong here:

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

Possibly the default route does not even accept CPF as 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 eye:

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 to the memory of the application, to select only one record. Nothing good in performance.

The second reason is simplicity.

Maybe this 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 whether data whole proceeds as follows:

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

That should do it.

  • vlw same... still not working, it is not finding the object in the action, but the function Jquery ta working... Thank you very much

  • What do you mean? You put a breakpoint in Action to test?

  • Yes, the Action is receiving the parameter, but the variable returns Null. If I put it the other way, it says q the enumeration did not generate results

Browser other questions tagged

You are not signed in. Login or sign up in order to post.