Form Zera values greater than 999.99

Asked

Viewed 114 times

2

I’m going through a problem I’ve never seen before. I’m hoping someone has seen it and can help me.

I’m working with Asp.net mvc 5, and sending a form via post to my controller.

My model:

<div class="col-md-6">
    <form action="@Url.Action("Create","DadosManuais")" method="get" class="form-horizontal">
meus campos aqui...
</div>

My controller:

public ActionResult Create(DadosManuaisCreate dados){
     Algumas linhas de código...
}

I’m also using jquery to create the masks in the model:

@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
        $(document).ready(function () {
$("#periodo").inputmask("mm/yyyy");
            $("#tacGestor").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#vlEsperado").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#vlAdquirido").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#selic").maskMoney({ showSymbol: true, symbol: "R$", decimal: 
                ",", thousands: "." });
            $("#premioSelic").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#seguro").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#despesas").maskMoney({ showSymbol: true, symbol: "R$", 
                decimal: ",", thousands: "." });
            $("#naoIdentificados").maskMoney({ showSymbol: true, symbol: 
                "R$", decimal: ",", thousands: "." });
            $("#qtdContratosAtivos").inputmask("9{1,3}.9{1,3}.9{1,3}");
        });
</script>   
}

Note that at the end of the jquery command, there is a parameter "thousands: "." }"

This says that for thousands, the point (".") will be used to divide.

My problem happens when I press the Ubmit button. Object is instantiated by parameter in the controller, but fields containing numbers greater than 999.99 are reset.

Look at you:

Erro

I removed the parameter "thousands: "." }" and the problem stopped occurring:

Erro 2

How to get around this problem without having to remove my mask?

Most grateful!

PS: No data posted here is real. They are all used to simulate a system insertion.

1 answer

1


You just need to format the data before to send to the controller, because the json parser used by it nay interprets the points used to divide the multiples of 1000.

So:

$( '#idDoForm' ).submit( () => {
   ///Quando o form for enviado
   let $inputs = $( '.formatar' );
   ///Coloque essa classe em todos os campos que vão zerados caso passem de 1000
   $inputs.each( (indice, input) => { 
       ///Para cada input com a classe 'formatar'
       let $input = $(input);

       ///Trocamos todos os pontos por nada
       $input.val( $input.val().replace(/\./g,''));
   })

   ///Submit
   return true;
})

Edit :

var submit = false;
$( '#idDoForm' ).submit( () => {
   ///Toda a logica de antes aqui...

   if(!submit)
       setTimeout( () => {
           submit = true;
           $( '#idDoForm' ).submit();
       },300);//Vamos dar um tempo para o jQuery mudar o form
   ///Submit
   return submit;

})
  • I was able to understand what might be going on, but it didn’t work :(. The script seems to be running after the controller code. I remember it previously worked, but something I changed made the data start to be zeroed.

  • @Csorgo edited the answer, there must be another way to solve this, but for now it must work like this.

  • Not yet :( Is there a solution in the back end in c#?

  • @Csorgo all the inputs have a name corresponding to a class property ?

  • Yes @I Dunno. The id too. I don’t know if there is any conflict between them, but I put to avoid many different references to the same element.

Browser other questions tagged

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