ASP.NET MVC 2 - 属性验证 [英] ASP.NET MVC 2 - Property validation

查看:197
本文介绍了ASP.NET MVC 2 - 属性验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC和有关于验证的问题。有没有办法动态设置错误信息?

I'm new to MVC and have a question regarding validation. Is there a way to dynamically set the Error Message?

例如,我怎么能实现以下(忽略硬$ C $ 50的CD,这可能来自于Web.config文件或者具体到当前登录)。

For example, how could I achieve the following (ignore the hardcoded 50, this could come from the Web.config or specific to the current logged).

[MetadataType(typeof(DocumentValidation))]
public partial class Document
{
    public class DocumentValidation
    {
        private const int MaxLength = 50;

        [Required(ErrorMessage = "Document Title is required")]
        [StringLength(MaxLength, ErrorMessage = "Must be under " + MaxLength.ToString() + " characters")]
        public string Title { get; set; }
    }

}

谢谢,

推荐答案

检查出的 IDataErrorInfo的以及这个问题我问了一下 IDataErrorInfo的与DataAnnotations

Check out IDataErrorInfo and this question I asked about IDataErrorInfo vs. DataAnnotations.

这篇关于ASP.NET MVC 2 - 属性验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆