asp.net-mvc 如何替换 Asp.net MVC 2 中的默认 ModelState 错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1538873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:53:24  来源:igfitidea点击:

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

asp.net-mvcfileresourcesmodelstate

提问by C.T.

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.

谢谢你。

回答by Max Toro

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

我不知道 v2,但这适用于 v1:

  1. Add a resource file in App_GlobalResources.
  2. In the resource file you can define strings named PropertyValueInvalidand PropertyValueRequired.
  3. On the Application_Start global.asax event set System.Web.Mvc.DefaultModelBinder.ResourceClassKey = "resource file name".
  1. 在 App_GlobalResources 中添加资源文件。
  2. 在资源文件中,您可以定义名为PropertyValueInvalid和 的字符串PropertyValueRequired
  3. 在 Application_Start global.asax 事件集上System.Web.Mvc.DefaultModelBinder.ResourceClassKey = "resource file name"

回答by C.T.

Got It.

知道了。

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

在 ASP.NET MVC 2 RC 中,它是PropertyValueInvalid,而不是InvalidPropertyValue

回答by AH.

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

尝试使用: <%= Html.ValidationMessage("Price") %> 不带星号“*”。

回答by Davi Fiamenghi

Cynthia, try to add this into your web.config

Cynthia,尝试将其添加到您的 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

MVC 调用httpContext.GetGlobalResourceObject(ResourceClassKey, "InvalidPropertyValue", CultureInfo.CurrentUICulture)查找消息,有时必须将那些提供程序添加到 GetGlobalResourceObject 中才能找到正确的资源