Passage of parameter C#

Asked

Viewed 779 times

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 don’t know how to send the js variable to the controller. The controller is already waiting but I don’t know how to do it.

  • 4

    Tried to var serviceURL = "/Solicitacao/ConsultaSolicitacao/" + id;?

  • This way it passes the link and not by parameter, hence the error. The link is http://localhost:18376/Request/Query

  • One bar was missing before the 2, read again my example.

  • Thank you very much

  • What if I need to pass 2 variables? I tried so but this giving error. var serviceURL = "/Solicitacao/UpdateDepartamentos/" + id_Departamento + id_Solicitacao; and so too var serviceURL = "/Solicitacao/UpdateDepartamentos/" + id_Departamento, id_Solicitacao;

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 don’t do this, you will be creating global variables inadvertently, which is not a good idea.

  • What if I need to pass 2 variables? I tried so but this giving error. var serviceURL = "/Solicitacao/UpdateDepartamentos/" + id_Departamento + id_Solicitacao; and so too var serviceURL = "/Solicitacao/UpdateDepartamentos/" + id_Departamento, id_Solicitacao;

  • Separate each one with bar: + id_Departamento + '/' + id_Solicitacao;. And, of course, in C# add another parameter to your method

  • I did the following var serviceURL = "/Solicitacao/UpdateDepartamentos/"+id_Departamento +'/'+ id_Solicitacao; and in my controller this way public void UpdateDepartamentos(int id_Departamento, int id_Solicitacao) but is giving error Not Found http://localhost:18376/Solicitacao/UpdateDepartamentos/2/1

  • @Viniciusvaz Then I no longer know (actually I have little experience with Asp.met mvc). I suggest posting a separate question about this case.

1

The correct to be done is to pass through an Array, sending by get, my code looks 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){}

1

You can pass this way too.

Id = getVar("id");

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

        },
        error: function () {

        }
});

Browser other questions tagged

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