3

Let me preface this by saying I have next to zero understanding of how mvc works, and most of my code was generated by visual studio. I don't even know where to look to solve this issue.

So, I created a database that includes 'price'(decimal(5,2)) as a field. When viewing the database, the price field is displayed in the format 0,00. The db has one record, with the price value as 5,25. I then had vis studio generate a model based on this database(and then controllers with views). For the most part it works; I can insert a new record via the view and store it in the db.

The issue is with the decimals. When I go to the default 'Create' view, the text field to enter price starts with the default value of 0,00. This text field then immediately shows red warning text "The field price must be a number". If I enter a number with no decimals, the record is inserted without issue. If I enter a number with a comma, I get the above mentioned warning. If I enter a number int the format 0.00(e.g 1.25) I get no warning, and can submit, but the number entered will be 0.

A similar problem is present in the default 'Edit' view. The text fields all auto-populate with data from the selected record(the one want to edit), but the price field displays in the format 0,00, and I get the same warning as above. When displaying records in the default 'Index' view, the record with 5,25 price displays correctly in the format 0,00.

I have zero attachment to either format, I just want consistency, and the ability to input numbers with decimals. Some of my code:

The input in the edit/create views (identical in both):

<div class="form-group">
    <h4>
        <strong>Price</strong>
    </h4>
    <input asp-for="ItemPrice" class="form-control" />
    <span asp-validation-for="ItemPrice" class="text-danger"></span>
</div>

The declaration of the price field in the model:

public decimal ItemPrice { get; set; }
Nino
  • 6,931
  • 2
  • 27
  • 42
jorkington
  • 103
  • 1
  • 5
  • 1
    Probably your front code (jquery) validation allows only `.` (dot) as a valid decimal separator and your asp net server culture is set to culture that uses `,` (comma) as a decimal separator. Here you will be able to find more information https://stackoverflow.com/questions/48066208/mvc-jquery-validation-does-not-accept-comma-as-decimal-separator – Marcin Maleszewski Sep 24 '19 at 11:42
  • Thanks for the help, the comments on the linked page were a bit over my head. I tried altering the jquery.validate.js, but it didn't seem to do anything. – jorkington Sep 24 '19 at 12:15
  • If you don't care about app culture then try to change culture target of your ASP.NET application to one that has dot representation (en-US for example) – Marcin Maleszewski Sep 24 '19 at 12:22
  • How(in what file, if it is a file) could I do this? – jorkington Sep 24 '19 at 12:28

1 Answers1

0

I had a project and that price issue happened to me too.

The question is Do you want show the price like 1,234.56 or 1234,56.

If you want the show like first format, I will suggest a jQuery http://plugin price-format.github.io/Jquery-Price-Format/

If you want the show like second format, you can create a method and replace the ,(comma) to .(dot) in JavaScript.

OnePage
  • 116
  • 1
  • 13