How to replace the default ModelState error message in Asp.net MVC 2?

喜夏-厌秋 提交于 2019-11-26 14:49:31

问题


I need to replace the model state resource (to another language).

I've seen some answers to the question above, but unfortunately I could'nt make it work. Any detailed answer or example would be appriciated.

Thank you.


回答1:


Got It.

In ASP.NET MVC 2 RC, It is PropertyValueInvalid, not InvalidPropertyValue.




回答2:


I don't know about v2, but this works on v1:

  1. Add a resource file in App_GlobalResources.
  2. In the resource file you can define strings named PropertyValueInvalid and PropertyValueRequired.
  3. On the Application_Start global.asax event set System.Web.Mvc.DefaultModelBinder.ResourceClassKey = "resource file name".



回答3:


Try using: <%= Html.ValidationMessage("Price") %> without the star "*".




回答4:


Cynthia, try to add this into your web.config

  <compilation debug="true" targetFramework="4.0">
      <buildProviders>
        <add extension=".resx" type="System.Web.Compilation.ResXBuildProvider"/>
        <add extension=".resources" type="System.Web.Compilation.ResourcesBuildProvider"/>
      </buildProviders>
  </compilation>

MVC calls httpContext.GetGlobalResourceObject(ResourceClassKey, "InvalidPropertyValue", CultureInfo.CurrentUICulture) to find the message, and sometimes you must add those providers to GetGlobalResourceObject find the correct resource



来源:https://stackoverflow.com/questions/1538873/how-to-replace-the-default-modelstate-error-message-in-asp-net-mvc-2

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