'Error when i try to save a decimal in my edit form

i have this field in my model :

[Required(ErrorMessage = "Il budget é richiesto")]
    [DataType(DataType.Currency)]
    [Display(Name = "Budget")]
    public decimal ContactBudget { get; set; }

when i save in my creation page the value 1000 all works fine.

when i try to edit, the value showed inside the textbox is not longer 1000 but 1000,00

<div class="form-group">
            <label asp-for="ContactBudget" class="control-label"></label>
            <input type="text" asp-for="ContactBudget" class="form-control" />
            <span asp-validation-for="ContactBudget" class="text-danger"></span>
        </div>

and when i try to save i got this error:

The field Budget must be a number.

if i try to change in 1000.00 i got thi other error :

The value '1000.00' is not valid for Budget.

but if i rewrite 1000 i can save the form.

What to do to allow also the value 1000,00 ?

Thanks



Solution 1:[1]

Entity Framework translates the Decimal variable type as Decimal(18, 0) in SQL.

so change your property attribute


    [DataType(DataType.Currency)]
    [Display(Name = "Budget")]
    [Precision(18, 2)]
    public decimal ContactBudget { get; set; }

and made migration. DatType currency just adds a currency sign.

if you still have problem with comma instead of dot then try to replace type text with type number

<input type="number" asp-for="ContactBudget" class="form-control" />

or you can try to use this data annotation instead

[Column(TypeName = "decimal(18,2)")]
public decimal ContactBudget { get; set; }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1