How to transfer a javascript variable to c# aspnet

Asked

Viewed 42 times

-3

I have a Java from an aspnet project I would like to know how to transfer the id of this variable to the code c#

 for (var i = 0; i < data.operadores.length; i++) {
                                options += "<option value= '" +
                                    data.operadores[i]['id'] +
                                    "'>" +
                                    data.operadores[i]['userName'] +
                                    "</option >";
                            }
    
    var id = data.operadores[0]['id'];

1 answer

0

Pass that id by ajax in your script (I’m using jquery in the example below):

<script>
  $("#btnXxx").click(function () {
    var request = $.ajax({
                    url: "/Home/Teste",
                    type: "POST",
                    data: { id: id },
                    dataType: "html"
                  });

    request.done(function (msg) {
      console.log(msg);
    });
  });
</script>

And then, in your Controller, create the function that will receive this id:

[HttpPost]
public async Task<string> Teste(string id)
{
   string msg = "ID: " + id;

   return msg ;
}

Browser other questions tagged

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