Ajax - Float type parameter

Asked

Viewed 361 times

0

Hello, I’m in need of a little help with ajax, I think it’s silly but I’m having a lot of difficulty and I found nothing related on the internet about it, I need to pass parameter ajax to my controller, however of type float, I created a json object to try to be able to receive the value type float, however neither with object nor with explicit value I can pass type float, only string and type int.

It follows below my function Ajax, where I create an object and send values, vlTotalLiquido, vlUnitario, are values of type float because it is money, but in that part disregard why jquery is not a typed language but the value is right as I wish to send, EXAMPLE: 2,00

var Objeto = new Object();
        Objeto.idProduto = idProduto;
        Objeto.IdPreVenda = parseInt($("#idPreVenda").val());
        Objeto.qtItem = parseInt($("#qtItem").val());
        Objeto.vlUnitario = parseFloat($("#vlUnitario").val());
        Objeto.prDesconto = parseFloat($("#prDesconto").val());
        Objeto.vlTotalLiquido = parseFloat($("#ValorComDesconto").val());    


$.ajax(
{

    url: "/ItPreVenda/SalvarItens",
    dataType: "json",
    type: "POST",
    data: Objeto,
    success: function (data) {
        if (data.Resultado > 0)
            alert('sucesso');
            ListarItens(idprevenda);
    }
});

Below follows the code of my controller, I am receiving in my Object all values type INT but the values type float cannot receive!

 [HttpPost]
        public ActionResult SalvarItens(ItPreVenda Objeto)
        {
            //ainda irei tratar os valores
            return Json(Objeto);
        }

In case anyone knows a way to do so Thank you and a happy new year to all!

  • Why don’t you take the parseFloat and send strings to the server? When do you have 2,00 in type Number this will be just 2.

  • And I think you should take a look here: http://answall.com/q/11018/129

1 answer

0


In fact, everything you put into JSON technically turns into a string that will be passed to your controller.

So the only thing you would need to do is convert this string to float in your backend.

  • Opa Andre all good guy? Thanks for the feedback, but I had this idea to use Convert.Tofloat, but when I get my string "2.00" and convert to float it returns me 200 and so I can not work with this value because it is money!

  • Beauty and victory?! Try working with decimal instead of float, because in this case Voce would need a few decimal places, and the decimal would be more appropriate... I’m not able to reproduce the output here with 200... Maybe because of the Region of my Visual Studio... Try working with Culture feature to correctly convert the value considering the "." as a decimal divisor.

  • Thank God all right, man I tried the decimal also but when I put my controller to receive the decimal value value value value value, it is not called, because the ajax has to pass all the values in the right way(I did not understand this detail of ajax) however nor activated my controller, I’ll see this conversation with Culture feature and give you a feedback

  • Put here the code block in which you are receiving the variables from ajax, please... Then maybe we can find the problem.

  • Up there, I get a json Object

  • Can’t you access past JSON properties? "];

Show 1 more comment

Browser other questions tagged

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