Question

Let's suppose we have a class Client:

  public class Client
  {
    public int ID { get; set; }
    public int Type { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string County { get; set; }
    public string Town { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }

    public string StudentName { get; set; }
    public string Birthdate { get; set; }

    public string Company { get; set; }

    public string Observations { get; set; }
   }

The type (field Type) of the client can be 1(person) or 2(company).

How can I add different required attributes for both cases?

I want for the first case (person) to have required attributes for the following fields: ID , TYPE, Name, Address and Email.

For the second case (company) I want to add required attributes for: ID , TYPE, StudentName, Address,Company and Email.

How can I do this?

Was it helpful?

Solution

There is a library called "MVC Foolproof Validation" for this: http://foolproof.codeplex.com/

OTHER TIPS

This could be something like this:

class Client
{
    [Required]
    public int ID { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Email { get; set; }
    public string Country { get; set; }
    public string Town { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Birthdate { get; set; }
    public string Observations { get; set; }
}

class Person : Client
{
    [Required]
    public string Name { get; set; }
}

class Company : Client
{
    [Required]
    public string StudentName { get; set; }
    [Required]
    public string Company { get; set; }

}

Btw you answered yourself: "I want person", "I want company" - this states that you want 2 different classes for this not a single one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top