RequiredIf条件验证属性 [英] RequiredIf Conditional Validation Attribute

查看:843
本文介绍了RequiredIf条件验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到去落实,做以下验证属性的最佳方式了一些建议。

I was looking for some advice on the best way to go about implementing a validation attribute that does the following.

示范

public class MyInputModel 
{
    [Required]
    public int Id {get;set;}

    public string MyProperty1 {get;set;}
    public string MyProperty2 {get;set;}
    public bool MyProperty3 {get;set;}

}

我想有一个值ATLEAST为prop1 prop2 prop3如果prop3是唯一的价值填补它,它不应该等于假。
我将如何去为这个写一个验证属性(S?)?

I want to have atleast prop1 prop2 prop3 with a value and if prop3 is the only value filled it it should not equal false. How would i go about writing a validation attribute(s?) for this?

感谢您的帮助!

推荐答案

您可以检出<一个href=\"http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx\">following博客文章为 [RequiredIf] 自定义验证属​​性的样本实现。它比较针对单个其他属性值,但你可以轻松地调整了的IsValid 的方法来满足您的要求。

You may checkout the following blog post for a sample implementation of a [RequiredIf] custom validation attribute. It compares against a single other property value but you could easily tweak the IsValid method to meet your requirements.

这篇关于RequiredIf条件验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆