Pass values via Ajax to ASP.NET MVC Controller

Asked

Viewed 213 times

-1

I want to pass values via ajax of a text field on my screen for my action, I am doing as follows:

$('#Regra').change(function () {
        if ($(this).val().trim != '') {
            var regra = $('#Regra').val();
            var tipoAtribuicao = $('#TipoAtribuicao').val();
            $.fiajax({
                url: urlTipoAtribuicao,
                type: 'POST',
                contentType: "application/json; charset=utf-8",
                data: { regra: regra, tipoAtribuicao: tipoAtribuicao },
                success: function (data) {
                    if (data.status && data.status == 'OK') {
                        console.log('entrou');
                        $('#DescricaoRegra').val(data.record.Descricao);
                    } else {
                        console.log('entrou no else');
                        if (data.error && data.error.Message)
                            alert(data.error.Message);
                    }
                }
            });

        }
            });

I want to pass the Rule field and the Type field, however it is giving error 500, I am doing something wrong ?

  • What kind of element is #TipoAtribuicao? It’s an input, a select...? And why are you using it contentType: "application/json; charset=utf-8"? Error 500 is server error. Where is this error? In Alert?

1 answer

0

Apparently your code is correct. Take a look at the action represented in your 'urlTipoAtribuicao'. See what it expects to receive as parameters and types of these parameters. See if the var rule = $('#Rule'). val() and var typeAtribuicao = $('#Tipoatribuicao'). val(); are receiving and passing the correct values. It is worth putting a break point in your method and debug to see where it will 'cry'.

  • Then, it even falls on the controller, but the first parameter comes value, 2 comes null even filling the field.

  • In this case, the second attribute would be var type Tribuicao = $('#Tipoatribuicao'). val() correct? takes a look at whether Tipoatribution is really an id or a class. In that case it would change from $('#Tipoatribution'). val() to $('.Tipoatribution'). val() Or if at some point this field with the id Typribution is repeating on your page and it’s picking up the value of the latter. It happens a lot with radio or checkbox fields. The problem is that there are many scenarios to evaluate, but these 2 are the most common.

  • Solved, it was the name of the parameter in the controller in c# did not know that it had to be the same name passed in JS, I thought that only the type sufficed kkkkk lack of attention was worth.

Browser other questions tagged

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