Error saving Decimal value with . Net Core and Mysql

Asked

Viewed 142 times

-1

In my project I have a field Valor of decimal type, as I set up the culture for en in my project I need to send the value with the tab ",". If I define the input as text does not accept sending with ",", only "." which in the database does not consider as a monetary separator. If I define the field of input as in kind number he accepts the "," but does not send or receive it when I get the return on Edit. What I tried was to give a replace in the "." turning into "," in sending, however I did not object success. It follows below my codes, if you have a better way of working with Decimal in . net core will be grateful if anyone can explain.

Example: If I send the value 12.02 or 12.02 it records in the bank 1202

View:

 <div class="form-group col-md-2">
      <label>Valor</label>
      <input type="number" step="any" class="form-control" id="valor" asp-for="Valor"/>
 </div>

Controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditarContrato(int ContratoId, Contratos contrato)
        {
            if (ContratoId != contrato.ContratoId)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                var Valor = Convert.ToDecimal(contrato.Valor).ToString().Replace(".", ",");

                contrato.Valor = Convert.ToDecimal(Valor);

                await _contratosRepositorio.Atualizar(contrato);
                return RedirectToAction("Contratos");
            }

            return View(contrato);
        }

Model:

public decimal Valor { get; set; }

Startup:

// Definindo a cultura padrão: pt-BR
            var supportedCultures = new[] { new CultureInfo("pt-BR") };
            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture(culture: "pt-BR", uiCulture: "pt-BR"),
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            });
  • 1

    in the backend, work with decimal only, and send decimal to the bank, don’t do this:: var Valor = Convert.ToDecimal(contrato.Valor).ToString().Replace(".", ","); One thing is the numeric value, another is the display on the front. Now on the front, try using the attribute lang to set the display in EN format

  • working directly with decimal I have the same problem of not recognizing "," or ".". about lang is already set to en in HTML, but in Edit it does not return me the value if the input field is of type number, only type text, already with the field type text I cannot send if I put "," in the value.

  • as @Rovannlinhalis commented, work with the decimal value, for this, do replace before converting, for example: var Valor = Convert.ToDecimal(contrato.Valor.Replace(",",".")); if the value has points of thousands, dollar dollar, etc, need to replace before tbm. For that already have questions here on the site, just search

  • @Ricardopoint doing straight so does not accept Replace, to use Replace I first do not need to convert into string? I did a lot of research here and on Google, and I couldn’t find any solution to this problem, the best I could was to set the language to "en" then I can record with "." but this gives me trouble in other returns, I would like to leave the default language in en

  • in the bank you will always record with points, only changes this when displaying to the user

  • @Rovannlinhalis the problem and it does not matter if I type with "." or "," anyway it does not recognize at the time of recording in the bank. When trying to put var Value = Convert.Todecimal(contract.Valor.Replace(",",".")); I get the error that "failed to resolve the "Replace" symbol".

Show 1 more comment

1 answer

0


I was able to find a simpler solution by personalizing the direct culture in the startup. Follow the resolution if anyone else has the same problem:

 // Setting the default culture: pt-BR
    var defaultDateCulture = "pt-BR";

    // Formatter number
    var ci = new CultureInfo(defaultDateCulture);
    ci.NumberFormat.NumberDecimalSeparator = ".";
    ci.NumberFormat.CurrencyDecimalSeparator = ".";

   // Configure the Localization middleware
   app.UseRequestLocalization(new RequestLocalizationOptions
   {
        DefaultRequestCulture = new RequestCulture(ci),
        SupportedCultures = new List<CultureInfo>
        {
             ci,
        },
        SupportedUICultures = new List<CultureInfo>
        {
             ci,
        }
        });

Browser other questions tagged

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