Model view controller 在MVC中验证复杂类

Model view controller 在MVC中验证复杂类,model-view-controller,asp.net-mvc-2,Model View Controller,Asp.net Mvc 2,考虑以下代码: public class AccountNumber { [AccountNumber] //This validator confirms the format of the account number public string Value {get; set;} public int Format { get; set;} public string ToString() { return Value + "

考虑以下代码:

public class AccountNumber
{
    [AccountNumber] //This validator confirms the format of the account number 
    public string Value {get; set;}

    public int Format { get; set;}

    public string ToString() 
    {
        return Value + " is format " + Format;
    }
}

public class MyViewModel
{
     public MyViewModel()
     {
          SourceAccount = new AccountNumber();
          DestinationAccount= new AccountNumber();
     }

     [Required]
     AccountNumber SourceAccount {get; set;}

     AccountNumber DestinationAccount {get; set;} 
}
然后,在我看来:

@Html.EditorFor(model => model.SourceAccount.Value)
@Html.EditorFor(model => model.DestinationAccount.Value)
基本上,我想说的是,用户必须输入一个源帐户,并且他们可以选择输入一个目标帐户。但是,如果他们确实输入了目的地帐户,则该帐户必须符合特定格式

上面代码的问题是SourceAccount上所需的验证器将始终返回valid,因为SourceAccount从不为null。什么是实现我想要实现的目标的好方法

请注意,在现实生活中,
的setter比显示的更复杂,因为它以规范格式重新格式化了帐号

编辑请注意,我们必须使用内置MVC验证,因为这是项目其余部分当前正在使用的

也许您想试试,它是数据注释属性的模型验证替代方案,允许您添加更复杂的模型验证逻辑

代码仍然非常简洁明了:

[Validator(typeof(PersonValidator))]
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}

public class PersonValidator : AbstractValidator<Person>
{
    public PersonValidator()
    {
        RuleFor(x => x.Id).NotNull();
        RuleFor(x => x.Name).Length(0, 10);
        RuleFor(x => x.Email).EmailAddress();
        RuleFor(x => x.Age).InclusiveBetween(18, 60);
    }
}
[Validator(typeof(PersonValidator))]
公共阶层人士
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串电子邮件{get;set;}
公共整数{get;set;}
}
公共类PersonValidator:AbstractValidator
{
公共PersonValidator()
{
RuleFor(x=>x.Id).NotNull();
RuleFor(x=>x.Name).Length(0,10);
RuleFor(x=>x.Email).EmailAddress();
(x=>x.Age)的规则。包括两人(18,60);
}
}
请参见。
这与内置MVC验证完全兼容


当然,您可以通过使用自己的界面进行验证来定制此解决方案

一种简单的方法是为SourceAccount和DestinationAccount编号添加简单的字符串属性,如下所示:

public class MyViewModel
{
    public MyViewModel()
    {
    }

    [Required]
    [AccountNumber]
    public string SourceAccountNumber { get; set; }

    [AccountNumber]
    public string DestinationAccountNumber { get; set; }

    public AccountNumber SourceAccount
    {
        get
        {
            return new AccountNumber
            {
                Value = SourceAccountNumber,
                Format = 0 // Set Format appropriately
            };
        }
    }

    public AccountNumber DestinationAccount
    {
        get
        {
            return new AccountNumber
            {
                Value = DestinationAccountNumber,
                Format = 0 // Set Format appropriately
            };
        }
    }
}

谢谢你的建议,尼科。不幸的是,我们必须使用MVC验证,因为项目目前使用MVC验证。我在问题中已经讲清楚了。