简体   繁体   English

我怎样才能要求多个属性中的一个?

[英]How can I require one of multiple properties?

Example: 例:

public class Person
{
    [Required]
    [DisplayName("Firstname")]
    public string firstname { get; set; }

    [Required]
    [DisplayName("Lastname")]
    public string lastname{ get; set; }

    // I want to require at least one of these 3 phones
    [DisplayName("Mobile Phone")]
    public string mobilephone { get; set; }

    [DisplayName("Work Phone")]
    public string workphone { get; set; }

    [DisplayName("Home Phone")]
    public string homephone { get; set; }
}

I want to have at least one phone number, but I don't care which one. 我想至少有一个电话号码,但我不在乎哪一个。 Entering more than one phone number should also be possible. 也应该输入多个电话号码。

Is there a clean way to do this? 有干净的方法吗? Or do I have to write the validation myself? 或者我是否必须自己编写验证?

Yes, you have to write the validation yourself. 是的,您必须自己编写验证。 Use IValidatableObject 使用IValidatableObject

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证多个对象属性,然后在我的一个表单元素上应用带有工具提示的样式? - How can I validate multiple object properties and then apply a style with a tooltip on one of my form elements? 如何将一对多可选关系转换为一对一的需求关系代码优先? - How can I convert a one to many optional relationship to a one to one require relationship codefirst? 如何在umbraco节点的一个选项卡中访问所有属性? - How can I access all the properties in one tab in an umbraco node? 如何使用EditorFor在一个字段中使用多个属性? - How can I use multi properties in one field using EditorFor? 如何创建具有属性列表之一的行列表 - How can I create a list of rows that have one of a list of properties 如何在C#中一次分配多个对象属性? - How can I assign multiple object properties at once in C#? 如何为多个BO属性定义IDataErrorInfo错误属性 - How can I define a IDataErrorInfo Error Property for multiple BO properties 我如何使用Linq进行多个属性的求和 - How can I multiple and sum some properties using Linq 如何以相对较短的方式编辑多个对象的属性? - How can I edit properties of multiple objects in a relatively shorter way? 如何将多个数组合并为一个? - How can I merge multiple Arrays into one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM