1
Hello, I am making a product registration using . Net Core 3.1 and I have the following problem: In the product register there is a field to insert the value of the product, however when inserting a value with a comma, 300,50 for example, when exiting the input, the following message is returned : The field Price must be a number.. What can I do to insert values with "," ?
Code:
Product Class - Preco field
[Required(ErrorMessage = "O campo <b>Preço</b> deve ser preenchido!")]
[Display(Name = "Preço")]
[DataType(DataType.Currency)]
public decimal Preco { get; set; }
In the Product class I make a select * from product id_Produto = @id_Produto and the attributes are fed as follows:
Produto produto = new Produto()
{
ProdutoId = Convert.ToInt32(rdr["id_Produto"]),
Descricao = rdr["descricao"].ToString(),
-> Preco = Convert.ToDecimal(rdr["preco"]),
Status = rdr["status"].ToString(),
Id_Categoria = Convert.ToInt32(rdr["id_Categoria"])
};
In the Edit.cshtml is like this:
<div class="form-group">
<label asp-for="Preco" class="control-label"></label>
<input asp-for="Preco" class="form-control"/>
<span asp-validation-for="Preco" class="text-danger"></span>
</div>
There is a way using
System.Globalization
of changing the decimal separator, gives a search. A practical solution: "35.50". Replace(",",".");– user178974
Good evening Edney, thanks for your help! I did as Leonardo said, the only thing different I needed to do was instead of using decimal.tryParse was to use Convert.Todecimal.
– Gabriel Longatti
PRICE.REPLACE(",",""). REPLACE(",","."); AND BE HAPPY
– wallafe sousa