Parameter passing C #

8

I have the following code in js

function consultaSolicitacao() {
id = getVar("id");
serviceURL = "/Solicitacao/ConsultaSolicitacao";
$.get(serviceURL, null, function (data) {
        var aux = data.length;
        var tblText = '';
        for (var i = 0; i < aux; i++) {
            var tmpArgs = data[i].id + ",'" + data[i].assunto
                    + "','" + data[i].mensagem + "','" + data[i].endereco + "','" + data[i].anexo + "'";
            var id = data[i].id;
            tblText += '<div id="Solicitacoes"><div>';
            tblText += '<span id="idSolicitacao">' + data[i].id + '</span>';
            tblText += '</div></div></a>';
        }
    document.getElementById("solicitacaoID").innerHTML = tblText;
});

}

I need to pass the value of my 'id' variable to the controller:

public ActionResult ConsultaSolicitacao(int id)
    {
        var x = Teste.ConultaSolicitacao(id);
        return new JsonResult()
        {
            Data = x, JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }

But I do not know how to send the js variable to the controller. The controller is already waiting but I do not know how.

    
asked by anonymous 13.05.2015 / 21:03

3 answers

7

Because your request uses the HTTP GET method, the parameter must be part of the URL itself. Then the URL will have the following format:

/Solicitacao/ConsultaSolicitacao/id

It seems simple to change your code to generate this:

function consultaSolicitacao() {
    var id = getVar("id");
    var serviceURL = "/Solicitacao/ConsultaSolicitacao/" + id;
    // resto do código
}

Notice that I also put var before the variable names. If you do not, you're inadvertently creating global variables, which is not a good idea.

    
13.05.2015 / 22:15
1

The correct thing to do is to pass through an Array, sending by get, my code stayed like this

function consultaSolicitacao() {
id = getVar("id");
var = parametros;
    parametros = { id_Departamento: id_Departamento, id_Solicitacao: id_Solicitacao }
serviceURL = "/Solicitacao/ConsultaSolicitacao";
$.get(serviceURL, parametros, function (data) {
        var aux = data.length;
        var tblText = '';
        for (var i = 0; i < aux; i++) {
            var tmpArgs = data[i].id + ",'" + data[i].assunto
                    + "','" + data[i].mensagem + "','" + data[i].endereco + "','" + data[i].anexo + "'";
            var id = data[i].id;
            tblText += '<div id="Solicitacoes"><div>';
            tblText += '<span id="idSolicitacao">' + data[i].id + '</span>';
            tblText += '</div></div></a>';
        }
    document.getElementById("solicitacaoID").innerHTML = tblText;
});

And the Controller is waiting for the parameters

    public ActionResult ConsultaSolicitacao(int id, int id_Solicitacao){}
    
21.05.2015 / 16:44
1

You can pass that way too.

Id = getVar("id");

$.ajax({
        url: '/Solicitacao/ConsultaSolicitacao',
        data: { id: Id},
        type: 'GET',
        success: function (dados) {

        },
        error: function () {

        }
});
    
21.05.2015 / 17:50