Unobtrusive DateTime? Validation in MVC4

前端 未结 5 1949
情深已故
情深已故 2021-02-08 08:21

I upgraded an MVC3 solution to MVC4. After the migration, the validator is broken.

My input date, if i select German as language, is \"20.03.2013\". I get an validation

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 09:02

    The point is that Mvc3 doesnt validate at all dates on the client side that is the point. You just set the cultrure on the server side....but your culture settings are not reflected at all on the client side...at least the Mvc engine doesnt do it automatically. The only way to handle properly dates and numbers on the client side with cultures that differs from English is to use a a javascript globalization library that is able to parse properly dates in all cultures, and to set the client side culture equal to the server side culture, then you have to redefine properly all validation methods to use globalized functions. Please read this post of my blog that clarifies how to handle properly globalization on the client side: http://www.dotnet-programming.com/post/2011/12/14/Globalization-Validation-and-DateNumber-Formats-in-AspNet-MVC.aspx

    Moreover, please dont confuse CurrentCulture with CurrentUICulture CurrentUICulture doesnt affect at all the way numbers or dates are handled, but only the resource files containing culture specifi resources such as localized strings.

    Finally, it is very unefficient to set the culture in the model binder, since the model binder is a recursive function so it si called hundreds of times during model reconstruction, and the culture setting operation is not a simple variable setting operation but it has a not negligible cost. It is better to write a global controller filter to handle culture setting (I always do this way) so the operation is performed just once per request

提交回复
热议问题