我想创建一个自定义验证属性,我想在其中将my属性的值与我的模型类中的另一个属性值进行比较.例如我在我的模型类中:
...
public string SourceCity { get; set; }
public string DestinationCity { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想创建一个自定义属性来像这样使用它:
[Custom("SourceCity", ErrorMessage = "the source and destination should not be equal")]
public string DestinationCity { get; set; }
//this wil lcompare SourceCity with DestinationCity
Run Code Online (Sandbox Code Playgroud)
我要怎么去那儿?
如果我有一个包含字段列表的搜索对象,我可以使用System.ComponentModel.DataAnnotations命名空间将其设置为验证搜索中至少有一个字段不为null或为空吗?即所有字段都是可选字段,但应始终输入至少一个字段.
asp.net validation asp.net-mvc data-annotations asp.net-mvc-2
我想使用ComponentModel DataAnnotations验证两个属性中至少有一个属性值.我的模型看起来像这样:
public class FooModel {
public string Bar1 { get; set; }
public int Bar2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想验证FooModel,以便需要Bar1 或 Bar2.换句话说,您可以输入一个,或另一个,或两者,但您不能将它们都留空.
我希望这对于服务器端和不显眼的客户端验证都有效.
编辑:这可能是重复,因为这看起来类似于我想要做的