How to replace the default ModelState error messag

2019-01-09 17:35发布

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.

4条回答
爷的心禁止访问
2楼-- · 2019-01-09 18:00

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楼-- · 2019-01-09 18:22

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

查看更多
我只想做你的唯一
4楼-- · 2019-01-09 18:23

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

查看更多
趁早两清
5楼-- · 2019-01-09 18:26

Got It.

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

查看更多
登录 后发表回答