Martin VU
Martin VU

Reputation: 151

ASP.NET Core razor decimal in form value not valid

In my model:

[Range(1, 100)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public decimal MyDecimal { get; set; }

In my cshtml file:

<input asp-for="MyModel.MyDecimal" class="form-control" step="0.01" type="number" value="0.00" />

It's not working: The value is not a number.

Upvotes: 0

Views: 3001

Answers (1)

Martin VU
Martin VU

Reputation: 151

In my model:

public decimal MyDecimal { get; set; }
[Range(1, 100)]
[DataType(DataType.Currency)]
[Column(TypeName = "decimal(18, 2)")]
public string MyDecimal_string { get; set; }

In my .cshtml file:

<input asp-for="MyDecimal_string" step="0.01" type="number" class="form-control" />

In my .CS file, I convert MyDecimal_string to decimal:

Mydecimal = decimal.Parse(MyDecimal_string, CultureInfo.InvariantCulture);

I am using .NET 5

Upvotes: 3

Related Questions