How to change 'data-val-number' message validation in MVC while it is generated by @Html helper

后端 未结 14 1732
面向向阳花
面向向阳花 2020-11-28 03:44

Assume this model:

Public Class Detail
    ...
    
    
         


        
14条回答
  •  旧时难觅i
    2020-11-28 04:16

    This is not gonna be easy. The default message is stored as an embedded resource into the System.Web.Mvc assembly and the method that is fetching is a private static method of an internal sealed inner class (System.Web.Mvc.ClientDataTypeModelValidatorProvider+NumericModelValidator.MakeErrorString). It's as if the guy at Microsoft coding this was hiding a top secret :-)

    You may take a look at the following blog post which describes a possible solution. You basically need to replace the existing ClientDataTypeModelValidatorProvider with a custom one.

    If you don't like the hardcore coding that you will need to do you could also replace this integer value inside your view model with a string and have a custom validation attribute on it which would do the parsing and provide a custom error message (which could even be localized).

提交回复
热议问题