0

I'm working with a C# MVC5 application. I'm having a problem using jqueryval while my application culture is es-CL. I have this attribute in my class model

[Display(Name = "Coeficiente B")]
[RegularExpression(@"^([-]?[\d]+(,[\d]+)?)?$", ErrorMessage = "error")]
[DisplayFormat(DataFormatString = "{0:#.##}")]
public float? coefB { get; set; }

and this code in the view for the model

@Html.EditorFor(model => model.Sensores_Piezometros.coefA)

the problem is that jqueryval is invalidating my data in the client side because it doesn't recognize the comma as part of the number. so i get this error message El campo Coeficiente B debe ser un número

I think that changing or overiding the jqueryval method to put a replace(".",",") is not the best option because i need my application to work with any culture, not es-CL only and cultures like en-US should not allow comma as decimal separator.

Edit: if i can disable the default jqueryval validation for a single atribute(coefB) it would be ok too because i'm already validating with my own regex.

Clamari
  • 353
  • 4
  • 17
  • Can't you use a string value in the view an then parse it to a float in your action? – DCruz22 Apr 21 '16 at 19:29
  • No, because i have a model, and in my model i have to receive a number for this field. – Clamari Apr 21 '16 at 20:00
  • Is a good practice to use `ViewModels` class for presentation data, Then you can parse it to your model. – DCruz22 Apr 21 '16 at 20:05
  • yes, but i have a lot of number type fields in my database, i think that change all my data types into strings using viewmodels is not a good solution. – Clamari Apr 21 '16 at 20:11
  • You don't have to change your types in the database, just in the Viewmodel. Is just presentation data, your DB and your model will remain equal. Look at [this](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) for details. – DCruz22 Apr 21 '16 at 20:22

0 Answers0