javascript result for method in parameterized controller

Asked

Viewed 83 times

2

I am trying to send the result of a Webapi that is consumed via javascript on the page to a method in the controller that will make a persistence with this data

this is javascript

$('#CodigoCep').blur(function() {
        var cep = $('#CodigoCep').val();
        
        $.getJSON("http://localhost:13943/cep/" + cep,
                function (data) {
                    $('#Logradouro').val(data.logradouro);
                    $('#Bairro').val(data.bairro);
                    $('#Cidade').val(data.cidade);
                    $('#Estado').val(data.estado);
                })
            .fail(function () {
                $('#Logradouro').val('');
                $('#Bairro').val('');
                $('#Cidade').val('');
                $('#Estado').val('');
            });
    });

this is the method in the controller:

[HttpPost]
private void CadastraCep(string modelo)
{            
   ... persistir modelo
}

how to take the result of the first ajax and send to this method ?

  • Do you want to take the result of this get and do a post for your controller? That’s it?

  • You want to send the value of data received by post at url?

1 answer

0

You will create a new call by passing the parameter data See the example below.

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

In this case, you will preferably need this data is a JSON object, and in the controller you will manipulate this Json object.

Here are some examples. https://api.jquery.com/jquery.post/

Browser other questions tagged

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