Reputation: 49
Let me intoduce you first into some details. I'm building a web application using ASP.NET Core Razor Pages with Entity Framework mainly for database.
I have a Product class with a property called Price.
Which is currently looking like that.
[Required]
[Column(TypeName = "decimal(18,2)")]
public decimal Price { get; set; }
Thats how the web form looks.
<div class="form-group">
<label asp-for="Product.Price"></label>
<input asp-for="Product.Price" class="form-control" />
<span class="text-danger" asp-validation-for="Product.Price"></span>
</div>
So im having two main problems.
Example:
Im setting price to 33.33 (which should be correct). Im sending a post request. And error occurs "The value '33.33' is not valid for Price."
2.When I set a price (only able to pass a integer value rn), price is being saved in database. (thats all like it should be, but let me explain further)
Example:
Im setting a price to 33. Price is being saved in database as 33,00.
Problem:
Now when im getting price back from the database is presented as 33,00. Is presented like that in web form where the client side validation and server side validation not allows for that value.
In a nutshell - Client side and server side not allows for price to look like that - 33,33. Server side not allows for price to look like that - 33.33. I can only pass integer value as a price to be successfully validated. Price saved in database looks like that 33,00, which making problems later with client side and server side validation.
For client side validation im using code below. Dont actually know how its working.
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
Upvotes: 0
Views: 1190
Reputation: 163
You need to use System.Globalization.CultureInfo.CurrentCulture while viewing the data in presentation side and use System.Globalization.CultureInfo.InvariantCulture for using data internally and storing in database.
The Values stored and fetched from database must be only string and you may be converting it to double , during conversion you need to use the cultureinfo
Upvotes: 2