Problems getting a Viewmodel with decimal values using Ajax

Asked

Viewed 18 times

1

When you Get using Ajax, viewModel is coming to the Controller with the fields reset. I’m creating the class manually, converting it and doing the GET. I cannot figure out the reason for the problem, whether I am "setting" the fields with values in wrong format or if there is a problem doing the stringfy. I have tried several ways and could not solve. Someone would know how to help me?

Thank you and a hug to all!

inserir a descrição da imagem aqui

Viewmodel:

public class FinanceiroParcelaMovimentoCalculosPadraoViewModel
{
    public decimal ValorASerPago { get; set; }

    public decimal ValorPago { get; set; }

    public decimal PercentualJuros { get; set; }

    public decimal PercentualMulta { get; set; }

    public decimal PercentualDesconto { get; set; }
}

Controller:

[HttpGet]
[Route("financeiro-parcela-movimento-gerenciar/financeiro-parcela-movimento-calcular-valores-padrao")]
public JsonResult CalcularValoresPadrao(FinanceiroParcelaMovimentoCalculosPadraoViewModel financeiroParcelaMovimentoCalculosPadraoViewModel)
{
    
    return Json(new { financeiroParcelaMovimentoCalculosPadraoViewModel = financeiroParcelaMovimentoCalculosPadraoViewModel });
}

JS:

var valorASerPago = $("#txt-financeiro-parcela-movimento-valor-a-ser-pago").val();
var valorpago = $("#txt-financeiro-parcela-movimento-valor-pago").val();

var financeiroParcelaMovimentoCalculosPadraoViewModel = {
    ValorASerPago: valorASerPago ? parseFloat(valorASerPago.replace(',', '.')) : 0,
    ValorPago: 0,
    PercentualJuros: 0,
    PercentualMulta: 0,
    PercentualDesconto: 0,

};

$.ajax({
    url: "/financeiro-parcela-movimento-gerenciar/financeiro-parcela-movimento-calcular-valores-padrao",
    type: "GET",
    data: JSON.stringify(financeiroParcelaMovimentoCalculosPadraoViewModel),
    traditional: true,
    success: function (data) {

       

    },
    error: function () {
        stopLoadGlobal();
        alert("Oops! Algo deu errado.");
        return false;
    }
});

1 answer

0


Hello, I believe the ideal and you do a post, by application security, if you have a form on the screen, just do so: serialize: if the form fields are the same as viewModel, it will be recognized.

function financeiroParcelaMovimento(idRecebido, controller) {

    console.log(idRecebido);
    e.preventDefault();


    var urlCompleto = "/" + controller + "/Adicionar/";
    var t = $("input[name='__RequestVerificationToken']").val();
    var formulario = $("#Formulario");

    $.ajax({
        method: "POST",
        headers:
        {
            "RequestVerificationToken": t
        },
        url: urlCompleto,
        data: formulario.serialize(),
        success: function (resultado) {
              console.log(resultado);

        }
    });

}

Browser other questions tagged

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