简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM