开发者

Custom MVC Error Message Localization

I want to localize my error messages on my model using attributes, such as RequiredAttribute and RangeAttribute. I'm using ASP.NET MVC2 in Visual Studio 2010.

This is really easy if my localized resources were in resx resource files... using ErrorMessageResourceName and ErrorMessageResourceType... however, I need to integrate it against a custom localization framework.

I have set the resourceProviderFactoryType on the globalization section in web.config, but MVC doesn't use this. It tries to access a static property on a resource file which returns the localized string.

This is the call stack...

System.ComponentModel.DataAnnotations.ValidationAttribute.SetResourceAccessorByPropertyLookup() +56576
   System.ComponentModel.DataAnnotations.ValidationAttribute.SetupResourceAccessor() +146
   System.ComponentModel.DataAnnotations.ValidationAttribute.get_ErrorMessageString() +12
   System.ComponentModel.DataAnnotations.ValidationAttribute.FormatErrorMessage(String name) +33
   System.Web.Mvc.DataAnnotationsModelValidator.get_ErrorMessage() +31
   System.Web.Mvc.RequiredAttributeAdapter.GetClientValidationRules() +46
   System.Web.Mvc.Html.ValidationExtensions.<ApplyFieldValidationMetadata>b__0(ModelValidator v) +10
   System.Linq.<开发者_C百科;SelectManyIterator>d__14`2.MoveNext() +238
   System.Web.Mvc.Html.ValidationExtensions.ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String modelName) +207
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, String expression, String validationMessage, IDictionary`2 htmlAttributes) +527
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression, String validationMessage, IDictionary`2 htmlAttributes) +82
   System.Web.Mvc.Html.ValidationExtensions.ValidationMessageFor(HtmlHelper`1 htmlHelper, Expression`1 expression) +75

Does anyone know if it's possible for me to somehow override how MVC retrieves the error message to display?

thanks k


I would create a custom class attribute that inherits from the standard Mvc HandleError class attribute...

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html


Does this answer help?

How to use DataAnnotations ErrorMessageResourceName with custom Resource Solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜