H
H
HeBonpoc2015-01-22 23:57:54
ASP.NET
HeBonpoc, 2015-01-22 23:57:54

Entity framework validation for decimal type. How can I make it skip a fractional value?

If separated by commas, then on the client:

The field Amount must be a number.

If through a dot, then the server answers
The value '123.5' is not valid for Sum

In the model:
[DisplayName("Сумма")]
        public decimal Sum { get; set; }

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2015-01-23
@HeBonpoc

For editing, use the string type . Convert to decimal before saving to base .
In my projects I use an auxiliary class that has methods for working with numbers and can understand any decimal separator. Specifically, for this situation, there is a ToDecimal method (the code is open, the license does not prohibit using it in parts, if that).

[DisplayName("Сумма")]
public string Sum { get; set; }

public decimal SumDec 
{ 
  get
  { 
    return Convertion.ToDecimal(this.Sum); 
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question