开发者

localizing mvc 3.0 validation client messages

At first sorry for Microsoft MVC team because of no simple solution for localizing data annotation validation messages. It takes me a lot of time and finally no simple solution found!

I finall开发者_C百科y decided to inherit from RequiredAttribute to do localization. so I did this :

public class MyRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    public MyRequiredAttribute()
        : base()
    {
        this.ErrorMessageResourceType = typeof(Resources.DataAnnotationDefaults);
        this.ErrorMessageResourceName = "Required";
    }
}

where I provided my localized messages in DataAnnotationDefaults.resx file. So I can use this simply

      [MyRequired]
    public int UnitCode { get; set; }

But the problem is: it doesn't apply in client-side and just in server-side. why? what did I miss?

It is amazing that the following line is ok and works in client-side too!

[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(DataAnnotationDefaults))] 
 public int UnitCode { get; set; }

I will be glad and appreciated if someone can help me on this.


You need to understand how validation works in MVC and .NET. DataAnnotation is a generic validation library which can be used in all kinds of applications and not just MVC.

Hence MVC contains different types of adapters for MVC which is used to add support for DataAnnotations. The client-side adapters are specifically made for the attributes defined in System.ComponentModel.DataAnnotations.

You therefore need to create your own adapters to get it working with your derived attributes. I've written a blog entry about it.

The easier approach is to use my localized metadata provider.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜