Jsonresult request with decimal type

Asked

Viewed 206 times

1

Because when sending a request with a decimal type (3) the "Point" is deleted, see in the field INSS which is a decimal value, for example: I type 1,69 when making a bind in the template the stitch is deleted and transformed into 1690 what should be 1.690 ?

inserir a descrição da imagem aqui

Here is the Controller (there in javascript I eliminated some fields to be cleaner and easier to understand):

inserir a descrição da imagem aqui

HTML

<input type="text" name="inss" id="inss" value="" class="form-control" />

Javascript

 $("#btnCadastrar").on("click", function () {
                 var _fornecedor = { 
                 "FornecedorID": $("#FornecedorID").val(),
                 "Imposto":[]
             };
             var _inss = parseFloat($("#inss").val().replace(",", ".")).toFixed(3);

             _fornecedor.Imposto.push({
                 "INSS": _inss
             });

             $.ajax({
                 url: "/Fornecedor/IncluirJSON",
                 type: "post",
                 dataType: "json",
                 contentType: "application/json; charset=utf-8",
                 processData: false,
                 data: JSON.stringify(_fornecedor),
                 success: function (data) {
                     window.location.href = "/Fornecedor/Index";
                 },
                 error: function (result) {
                     alert(result.responseText);
                 }
             });
         });

MODEL

public class Imposto
    {
        [HiddenInput(DisplayValue = false)]
        public int ImpostoID { get; set; }
        public int FornecedorID { get; set; }
        public decimal? INSS { get; set; }
        public virtual Fornecedor Fornecedor { get; set; }
    }
  • As far as I know the decimal is written with . (Point) in C#, ie you should pass 1.69 instead of 1.69.

1 answer

1

Solution:

var _inss = $("#inss").val().replace("%", "").replace(",", ".") == '' ? "0.00" : parseFloat($("#inss").val().replace("%", "").replace(",", ".")).toFixed(3);

and then:

_fornecedor.Imposto.push({
            "INSS": JSON.parse(_inss)
        });

Browser other questions tagged

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