0

In my application I have a form where a user can create a new part.

The form for creating the part contains basic things like ID, Name, Description etc. However I am having some trouble with the Price field.

The Price field is set to a decimal:

[Table("pricelist")]
public class Pricelist
{
    [Key]
    [Required]
    public int Price_id { get; set; }

    public string CategoryId { get; set; }
    public string SubCategoryId { get; set; }
    public string AdminDescription { get; set; }
    public string AdminDescriptionShort { get; set; }
    public string Standard { get; set; }
    public string Unit { get; set; }
    public decimal? Quantity { get; set; }

    public decimal Price { get; set; }

But when I type in a value like 50,12, it gives me a ValidationMessage that sais: "The field Price must be a number."

It does not give me an Error however.

Here is the form field:

<div class="form-group">
    @Html.Label("Prijs", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
    </div>
</div>

What I find weird is that it does work in another form.

Other form:

<div class="form-group">
    @Html.Label("Prijs", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
    </div>
</div>

I have tried setting it to a float before but that did not work out. I don't think the Error from this attempt to fix it matters since I found out that what goes wrong in one form, works in the other form.

Luca
  • 296
  • 2
  • 19
  • What is the culture on your server And are you getting this as a client side message (in which case you need to reconfigure the `$.validator` to accepts numbers with the comma as a decimal separator –  Mar 23 '18 at 08:51
  • @StephenMuecke The culture of the server is Neutral and the messige is client side. But it works in the second form with a comma, just not in the first form. – Luca Mar 23 '18 at 08:57
  • Even if you reconfigure the validation as per the dupe, you still going to need to set the server culture to one that accepts numbers with a comma as the decimal separator (or create a custom ModelBinder) otherwise a server side error will occur –  Mar 23 '18 at 09:00

0 Answers0