-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
});
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 attributelang
to set the display in EN format– Rovann Linhalis
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.
– João
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– Ricardo Pontual
@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
– João
in the bank you will always record with points, only changes this when displaying to the user
– Rovann Linhalis
@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".
– João