相关疑难解决方法(0)

自定义验证属性,用于将my属性的值与模型类中的另一个属性值进行比较

我想创建一个自定义验证属性,我想在其中将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)

我要怎么去那儿?

c# asp.net-mvc validationattribute razor

55
推荐指数
2
解决办法
7万
查看次数

用于验证的数据注释,至少一个必填字段?

如果我有一个包含字段列表的搜索对象,我可以使用System.ComponentModel.DataAnnotations命名空间将其设置为验证搜索中至少有一个字段不为null或为空吗?即所有字段都是可选字段,但应始终输入至少一个字段.

asp.net validation asp.net-mvc data-annotations asp.net-mvc-2

31
推荐指数
2
解决办法
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.换句话说,您可以输入一个,或另一个,或两者,但您不能将它们都留空.

我希望这对于服务器端和不显眼的客户端验证都有效.


编辑:这可能是重复,因为这看起来类似于我想要做的

c# validation unobtrusive-validation asp.net-mvc-3

7
推荐指数
1
解决办法
2475
查看次数