How to call a View by passing parameters through json

Asked

Viewed 325 times

3

I have this code that passes parameters to my controller:

var id = $(Musico).val();
var url = '@Url.Action("Votar","Chamada")';
var tipo = 1;
$.ajax({
    url: url,
    data: {
         id:id, tipo:tipo
    }
});

my controller:

    public ActionResult Votar(int id, int tipo)
    {
        if (tipo == 1)//Tipo Musico
        {
            var chamadaMusicas = db.ChamadaMusicas.Include(c => c.Chamada).Include(c => c.Musica).Where(c => c.Chamada.PessoaID.Equals(id)).Where(i => i.Chamada.Ativa.Equals(true)).ToList();
            return View(chamadaMusicas);//Fábio Souza);
        }
        else//Local
        {
            var chamadaMusicas = db.ChamadaMusicas.Include(c => c.Chamada).Include(c => c.Musica).Where(c => c.Chamada.LocalID.Equals(id));
            return View(chamadaMusicas.ToList().Where(i => i.Chamada.Ativa.Equals(true)));//Fábio Souza);
        }
    }

up to the controller, you’re getting the information all right, but the view doesn’t open. what you’d have to do for the Vote view is open?

1 answer

5


You are not treating the return of the Ajax call.

You have to deal with Success, take the result and insert somewhere.

        $.ajax({
        url: url,
        type: "GET",
        success: function (result) {
            $('#resultado').html(result);
        },
        error: function (x, m, s) {
            if (x.status == 403) {
                alert(m, x.statusText);
            } else {
                alert(x.responseText);
            }
        }            
    });

Browser other questions tagged

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