ASP.NET MVC 3数据注释:添加​​验证动态 [英] ASP.NET MVC 3 Data Annotation: Add validation dynamically

查看:98
本文介绍了ASP.NET MVC 3数据注释:添加​​验证动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的数据诠释。我想知道,如果有可能(以及如何)来动态添加一些验证。这是非常广泛的,解释为什么,但创造了当我接收一个视图模型和对象。在该对象我必须检查某些属性,并根据其价值,我应该已经或不是一些验证。

一个例子:

 公共类ProfileViewModel
{
    [必需(的ErrorMessage =字段{0}要求)]
    [显示(名称=客户端code)]
    公众诠释客户code {搞定;组; }    [必需(的ErrorMessage =字段{0}要求)]
    [StringLength(100的ErrorMessage =字段{0}必须有最多100个字符。)
    [显示(名称=公司)]
    公共字符串公司{搞定;组; }    [StringLength(50的ErrorMessage =字段{0}必须有超过50个字符。)
    [显示(名称=名称)]
    公共字符串名称{;组; }    [StringLength(50的ErrorMessage =字段{0}必须有超过50个字符。)
    [显示(名称=姓氏)]
    公共字符串名字{获得;组; }    公共ProfileViewModel(用户USR)
    {
        如果(usuario.Client code!= NULL)
        {
            客户端code = Convert.ToInt32(usr.Client code);
        }
        其他
        {
             //还没有要求客户端code和公司。
             //现在需要的名称和姓氏。
        }
        公司= usr.Company;
        NAME = usr.Name;
        姓氏= usr.LastName;
    }
}


解决方案

我觉得我想要做的最简单的方法是实施<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.ivalidatableobject.validate%28v=vs.110%29.aspx\"相对=nofollow> IValidatableObject

 公共类产品:IValidatableObject
{
    公众诠释为prop1 {搞定;组; }
    公众诠释Prop2 {搞定;组; }    公共IEnumerable的&LT;&为ValidationResult GT;验证(ValidationContext validationContext)
    {
        如果(为prop1&LT; Prop2)
            产量返回新的ValidationResult(财产1不能小于房产2);
    }
}

参见:<一href=\"http://weblogs.asp.net/scottgu/class-level-model-validation-with-ef-$c$c-first-and-asp-net-mvc-3\"相对=nofollow>类级别模型验证与... ASP.NET MVC 3

I'm new with data annotation. I'd like to know if it possible (and how) to add some validation dynamically. It is very extensive to explain why, but I've a ViewModel that receives and object when created. In that object I must check for some property and depending its value I should have or not some validations.

An example:

public class ProfileViewModel
{
    [Required(ErrorMessage = "The field {0} is required")]
    [Display(Name = "Client Code")]
    public int ClientCode { get; set; }

    [Required(ErrorMessage = "The field {0} is required")]
    [StringLength(100, ErrorMessage = "The field {0} must have up to 100 characters.")]
    [Display(Name = "Company")]
    public string Company { get; set; }

    [StringLength(50, ErrorMessage = "The field {0} must have up to 50 characters.")]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [StringLength(50, ErrorMessage = "The field {0} must have up to 50 characters.")]
    [Display(Name = "LastName")]
    public string LastName { get; set; }

    public ProfileViewModel(User usr)
    {
        if (usuario.ClientCode != null)
        {
            ClientCode = Convert.ToInt32(usr.ClientCode);
        }
        else
        {
             //ClientCode and Company are not yet required.
             //Name and LastName are now required.
        }
        Company = usr.Company;
        Name = usr.Name;
        LastName = usr.LastName;
    }
}

解决方案

I think that the simplest way of doing what I wanted is implementing IValidatableObject:

public class Product : IValidatableObject
{
    public int Prop1 { get; set; }
    public int Prop2 { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Prop1 < Prop2)
            yield return new ValidationResult("Property 1 can't be less than Property 2");
    }
}

See also: Class-Level Model Validation with ... ASP.NET MVC 3

这篇关于ASP.NET MVC 3数据注释:添加​​验证动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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