title: asp.net core 2.0 bind model with decimal value
body: <p>I've been looking for a solution to this case for hours, and I did not find anything that worked out for me.</p>
  
  <p>Here we use the comma for decimal separator, and when I send a value like "15,50", in my controller I get the value of the model as "1550", and it only makes sense if I send "15.50".</p>
  
  <p>I covered the following topics, and nothing worked.</p>
  
  <p><a href="https://stackoverflow.com/questions/40442444/set-cultureinfo-in-asp-net-core-to-have-a-as-currencydecimalseparator-instead">Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,</a></p>
  
  <p><a href="https://stackoverflow.com/questions/39737607/aspnet-core-decimal-binding-not-working-on-non-english-culture">Aspnet Core Decimal binding not working on non English Culture</a></p>
  
  <p>I am sending the form with ajax, using the <code>$form.serializeArray()</code> as below:</p>
  
  <pre><code>function getFormData(xform) {
      var $form = $('#' + xform);
      var unindexed_array = $form.serializeArray();
      var indexed_array = {};
  
      $.map(unindexed_array, function (n, i) {
          indexed_array[n['name']] = n['value'];
      });    
      return indexed_array;
  }
  </code></pre>
  
  <p>send:</p>
  
  <pre><code>function PostFormAjax(controller, metodo, params, redir, divacao, reloadgrid) {
  
      if (params != null) {
          var formData = new FormData();
  
          var json = $.parseJSON(JSON.stringify(params));
          $(json).each(function (i, val) {
              $.each(val, function (k, v) {
                  console.log(k + " : " + v);
                  formData.append(k, v);
              });
          });
      }
  
      $.ajax({
          url: '/' + controller + '/' + metodo,
          data: JSON.stringify(params),
          contentType: 'application/json',
          type: 'POST',
          success: function (data) {
              AjaxFim(metodo, data, redir, divacao, reloadgrid);
          }
      });
  }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>    [HttpPost]
      public IActionResult GravaProduto([FromBody] Produtos produto)
      {
          if (ModelState.IsValid)
          {
           //produto.Valorcusto is 1550 and not 15,50 as it was posted
           //produto.Valorvenda is 1550 and not 15,50 as it was posted
          }
      }
  </code></pre>
  
  <p>My Model.cs</p>
  
  <pre><code>public partial class Produtos
  {
      public int Cod { get; set; }
      public string Descricao { get; set; }
      public decimal Valorcusto { get; set; }
      public decimal Valorvenda { get; set; }
  }
  </code></pre>
  
  <p>I tried to make a replace in <strong><code>formData.append(k, v.replace(',', '.'));</code></strong> and also does not work, I also would not know which field is a decimal.</p>
  
  <p>What do I have to do? because I'm lost, what should have been simpler became more complicated.</p>
  
  <p>What I did to keep working until I found a solution was:</p>
  
  <p>Wear a mask according to my regional setting:</p>
  
  <pre><code>$('.stValor').mask("#.###.##0,00", { reverse: true });
  </code></pre>
  
  <p>And before posting the form, I switch to the accepted formatting:</p>
  
  <pre><code>$('#fCadAltProd').validator().on('submit', function (e) {
      if (e.isDefaultPrevented()) {
      } else {
          e.preventDefault();
          $('.stValor').mask("#,###,##0.00", { reverse: true });
          //Ajax post here
      }
  });
  </code></pre>