Open View with Parameters

Asked

Viewed 392 times

4

I have this Script:

$('#Musico').change(function () {
    var id = $(Musico).val();
    var url = '@Url.Action("Votar","Chamada")';
    var tipo = 1;

    $(function ChamaVotar() {
        $.post(url, { id: id, tipo: tipo });
    });//Function ChamaVotar

});//musico change

And this 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);
    }
    else//Local
    {
        var chamadaMusicas = db.ChamadaMusicas.Include(c => c.Chamada).Include(c => c.Musica).Where(c => c.Chamada.LocalID.Equals(id)).Where(i => i.Chamada.Ativa.Equals(true)).ToList();
        return View(chamadaMusicas);
    }
}

I need, when passing my parameters to the controller, to open the view. But it is not opening... The controller receives the information but does not open the page. What solution for this?

  • I do not know if it is the most appropriate way to do this, should put a form in the view that takes the parameters and give a Ubmit, this way you are only giving the post, there is no instruction in your javascript to redirect to the view returned by the controller.

2 answers

4

Try to build the url this way:

var url = '@Url.Action("Action", "Controller")' + '?id=' + id + '&tipo=' + tipo;

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'html',
    success: function (data) {
         // faz alguma coisa
    }
});

Note: I am editing the answer because I was wrong.

  • Alexadre good morning, yesterday to the 45 of the second time, I managed to solve similarly to this, I was so exhausted that I did not answer my own question... but the idea is this. Thanks

4

The solution I found to solve my problem was the following way, where the id and tipo were passed by parameters via URL. For the window to be opened I used the window.location.replace (which opens in the same window without opening a new tab).

$('#Musico').change(function () {
    var id = $(Evento).val();
    var url = '@Url.Action("Votar", "Chamada")';
    var tipo = 2;
    window.location.replace(url + '?id=' + id + '&tipo=' + tipo);

});
  • Blz, but using windows.Ocation, Voce will reload the screen, right? If you use the solution I posted, within Success Voce you can update your page or send a confirmation popup to the user without refreshing the screen. Of course, it depends on your need, just the suggestion.

  • I do not know if I am wrong, is that on the screen that step the parameters is one, the one that receives is another, so I think it would be necessary to open a new page. I’m still learning and I’m not sure how to handle these things. This is a project for the College and the time is short for us rsrsrs... the important thing is to work kkkk But it was really worth Alexandre

Browser other questions tagged

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