无论是有条件或验证在asp.net MVC2 [英] conditional either or validation in asp.net mvc2
本文介绍了无论是有条件或验证在asp.net MVC2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的注册页面我有陆地线路电话号码和手机号码领域。
In my registration page I have land line phone number and mobile number fields.
我需要确保用户需要添加至少一个电话号码或陆地线路或移动
I need to ensure that the user needs to add at least one phone number either the land line or mobile.
我如何做到这一点?
谢谢
ARNAB
Thanks Arnab
推荐答案
您可以编写一个自定义验证属性,并用它装点你的模型:
You could write a custom validation attribute and decorate your model with it:
[AttributeUsage(AttributeTargets.Class)]
public class AtLeastOnePhoneAttribute: ValidationAttribute
{
public override bool IsValid(object value)
{
var model = value as SomeViewModel;
if (model != null)
{
return !string.IsNullOrEmpty(model.Phone1) ||
!string.IsNullOrEmpty(model.Phone2);
}
return false;
}
}
和则:
[AtLeastOnePhone(ErrorMessage = "Please enter at least one of the two phones")]
public class SomeViewModel
{
public string Phone1 { get; set; }
public string Phone2 { get; set; }
}
有关更高级的验证场景你可能看看 FluentValidation.NET 或的万全
For more advanced validation scenarios you may take a look at FluentValidation.NET or Foolproof.
这篇关于无论是有条件或验证在asp.net MVC2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文