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

4

6

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.

Stifling answered 8/10, 2009 at 16:17 Comment(0)
A
9

Got It.

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

Arum answered 22/1, 2010 at 4:52 Comment(0)
T
12

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".
Thumbsdown answered 8/10, 2009 at 22:30 Comment(2)
Thank you for your answer, but for some reason it doesn't work. Maybe there is a different way to do it on mvc 2.Stifling
You can find out using Reflector.Thumbsdown
A
9

Got It.

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

Arum answered 22/1, 2010 at 4:52 Comment(0)
L
2

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

Luau answered 1/5, 2010 at 15:15 Comment(1)
I consider this one the best approach. (+1). User messages belongs to Views, not to Controller. If in future you want to create the same page in another language, you will keep the same controllers and change ONLY the views.Amin
P
1

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

Polyhistor answered 3/3, 2011 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.