jQuery - ASP.NET - Fields not accepting values with comma separation and ignoring decimal places

Asked

Viewed 22 times

1

For example, if I type 52,400 the system saves as 52,40 or if I type 5,000, it saves 5,0.
I’ve tried everything, but I can’t understand why the system is suppressing these decimal places.

My code

public class EstoqueDaEmbarcacao : Entity
{
    public decimal AguaCarregada { get; set; }

    public decimal DieselCarregado { get; set; }
}

jQuery

function preencherDadosEstoque(barcoId) {
    $("#estoque-barco-id").val(barcoId);
    $("#estoque-barco-nome").val($("#selectEmbarcacao option:selected").text());
    $("#modal-estoque").modal('show');
}
function salvarEstoque() {
    $("#message-estoque").removeClass("alert-danger");
    $("#message-estoque").removeClass("alert-warning");
    var barcoId = $("#estoque-barco-id").val();
    var agua = $("#estoque-barco-agua").val().replace(",", ".");
    var diesel = $("#estoque-barco-diesel").val().replace(",", ".");
    var data = JSON.stringify({ AguaCarregada: parseFloat(agua), DieselCarregado: parseFloat(diesel), BarcoId: barcoId });
    if (agua == "" || diesel == "") {
        $("#message-estoque").addClass("alert-warning");
        $("#message-estoque").html("Preencha todos os campos para continuar");
        return;
    }
    $.ajax({
        url: "/InfoApontamento/AtualizarEstoqueBarco",
        type: "POST",
        dataType: "json",
        data: data,
        contentType: "application/json",
        success: function (result) {

            if (!result.Success) {

                $("#message-estoque").html(result.ErrorDatail);
                $("#message-estoque").addClass("alert-danger");
            }
            else if (!result.Data) {
                $("#message-estoque").html(result.Message);
                $("#message-estoque").addClass("alert-danger");
            }
            else {
                alert("Salvo com sucesso!");
                $("#modal-estoque").modal('hide');
                $('#estoque-barco-agua').val("");
                $('#estoque-barco-diesel').val("");
                // abrirModalManutencao();
                preInicializarModal();
            }
        }
    });

}

page

<div class="col">
  <label for="embarcacaoInput">Água</label>
      <input type="number" step="0.01" class="form-control" id="estoque-barco-agua">
 </div>
  <div class="col">
   <label for="embarcacaoInput">Diesel</label>
      <input type="number" step="0.01" class="form-control" id="estoque-barco-diesel">
   </div>
 </div>
  • left zeros for decimal type have no value.. If you need to save all decimals including zeros, use text (string), float or double.. I believe string solves your problem

No answers

Browser other questions tagged

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