The field Sale Price must be a number. MVC

Asked

Viewed 1,616 times

1

I have a field, price sale, that model is like this:

  public decimal PrecoVenda { get; set; }

In Viewmodel he is like this:

[Display(Name = "Preço de Venda")]
    [Required(ErrorMessage = "O campo {0} é obrigatorio.")]
    public decimal PrecoVenda { get; set; }

And this is View:

 <label asp-for="PrecoVenda" class="col-md-2 control-label"></label>
                <div class="col-md-2">
                    <input asp-for="PrecoVenda" class="form-control" placeholder="Insira o  preço de venda.">
                    <span asp-validation-for="PrecoVenda" class="text-danger"></span>
                </div>

But when filling the values with comma, for example "0.00" He returns this message to me:

The field Sale Price must be a number.

If I put "25.00", when checking the value is "2500,00"

  • Try adding this annotaton date: [Datatype(Datatype.Currency)]

  • Same thing happens, same mistake occurs.

1 answer

2


I managed to solve, following the tutorial of this link

I install that package:

Install-Package Microsoft.AspNet.Mvc.pt-br

jQuery Valitation is responsible for client-side validation, to globalize it for PT-Br we need to create a new file in the "Scripts" folder called "methods_pt.js" with the following content:

   /*
 * Localized default methods for the jQuery validation plugin.
 * Locale: PT_BR
 */
jQuery.extend(jQuery.validator.methods, {
    date: function (value, element) {
        return this.optional(element) || /^\d\d?\/\d\d?\/\d\d\d?\d?$/.test(value);
    },
    number: function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});

And call the script on the page I need last.

bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*",
                    "~/Scripts/methods_pt.js"));

I hope I can help ;)

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • Thanks for the tip, I just updated with the code part.

  • Hello @Mariana!!! I’m having the same problem with decima fields... I did as you mentioned in the example and solved the problem... It didn’t work for dates... I noticed that in Fields type Date , if you inform a date, when clicking outside the input, appears an error msg asking to type the date... You know how to fix it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.