MVC Localization of Default Model Binder

早过忘川 提交于 2019-11-30 14:36:32

See http://forums.asp.net/p/1512140/3608427.aspx, scroll down to Brad Wilson's reply near the bottom of that page (Sat, Jan 09 2010, 3:20 PM). There are static properties on the DefaultModelBinder that you can set to localize the generic error messages.

The reason a generic error message is used instead of your [Range] message is that [Range] provides a validation error message, but this particular case is a binding error. There's absolutely no way the framework can ever hope to convert the string "ten" to an Int32, so it can't even fire the [Range] validator. This is what the "PropertyValueInvalid" key as mentioned in that forum controls.

In MVC3 do the following to change default messages:

  1. add the App_GlobalResources folder to your ASP.NET site
  2. add a new resource file, call it f.ex. MyResources.resx
  3. add these keys
    • PropertyValueRequired: A value is required.
    • PropertyValueInvalid: The value '{0}' is not valid for {1}.
  4. in Application_Start of global.asax.cs add the line DefaultModelBinder.ResourceClassKey = "MyResources";

Have you tried: IDataErrorInfo property

This article will help

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!