如何覆盖用于验证的属性

时间:2015-02-09 00:56:18

标签: c# asp.net asp.net-mvc

如果我有一个ViewModel类,它在其他ViewModel类中用作属性。它有一个编辑器模板,可以呈现日,月和年的下拉列表,并获取和设置日期值。我想在它使用的类中对此ViewModel执行验证,并公开其日期值以进行验证。

这需要适用于RequiredAttribute类等。

我似乎找不到覆盖用于验证的属性/值的方法。我想像会有类似于WebForms验证的东西,你可以使用' ValidationProperty'来装饰这个类。属性。

这样的事情:

[ValidationProperty("Value")]
public class DateViewModel
{
    public int Day{ get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
    public DateTime? Value { get {...} }
}

public class PersonViewModel
{
    [Required(ErrorMessage = "Please specify your birth date")]
    public DateViewModel BirthDate { get; set; }
}

public class AdViewModel
{
    [Required(ErrorMessage = "Please specify an end date for your ad")]
    [SomeCustomValidation(ErrorMessage = "The ad end date must be in the future")]
    public DateViewModel EndDate { get; set; }
}

0 个答案:

没有答案