在MVC中对复杂模型进行万无一失的验证

时间:2019-03-27 16:00:37

标签: c# asp.net-mvc model-view-controller foolproof-validation

在下面的代码中,我想根据某些单选按钮的值为true或为某些文本框添加错误来添加RequireIfTrue属性。

    public class Test{ 

    public string BranchOption { get; set; }

    public ClientAddressModel OldAddress { get; set; }

    public ClientAddressModel NewAddress { get; set; }
    }


    public class ClientAddressModel
    {   

        public string Country { get; set; }

        public string County { get; set; }

        public string City { get; set; }

        public string Street { get; set; }

        public string Number { get; set; }

        public string Block { get; set; }

        public string Stair { get; set; }

        public string Floor { get; set; }
}

单击“新地址”单选按钮时,我要根据分支选项来验证“国家/地区”,“县”和“城市”属性,例如:

RequireIfTrue("BranchOption")  
public string Country { get; set; }

问题在于BranchOption在测试模型中。

如果我这样说:

RequireIfTrue("BranchOption")
 public ClientAddressModel NewAddress { get; set; } 

正在验证NewAddressModel中的所有属性,而我不希望那样。

有没有办法做到这一点? 预先感谢。

0 个答案:

没有答案