Display/Edit a Currency in ASP.NET MVC Core, why so complicated?

后端 未结 2 762
傲寒
傲寒 2021-01-14 03:20

In an ASP.NET Core 2.0 application, I have a Foo class with a lot of classical string or numeric members and also a int? Budget fi

2条回答
  •  心在旅途
    2021-01-14 03:24

    My biggest finding was to ensure you are not just rendering the field via your own tag within the .cshtml; rather, ensure you are using the @Html.EditorFor() syntax to render the field:

                    
    @Html.EditorFor(model => Model.AmountPaid)

    Once I used this, the validation worked correctly. You can also decorate your field, such as:

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

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/validation?view=aspnetcore-2.2

提交回复
热议问题