title: MVC 5 ASP.NET Street Address Validation
body: <p>I have the following Profile class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace MyValidation.Models
  {
      public class Profile : ValidationAttribute
      {
  
          [Required]
          public string Name
          {
              get;
              set;
          }
  
          [Required]
          [Range(5, 99)]
          public int Age
          {
              get;
              set;
          }
  
  
          public string Street
          {
              get;
              set;
          }
          public string City
          {
              get;
              set;
          }
  
          public string Zip
          {
              get;
              set;
          }
  
          public string State
          {
              get;
              set;
          }
      }
  }
  </code></pre>
  
  <p>And View:</p>
  
  <pre><code>@model MyValidation.Models.Profile
  
  @{
      Layout = null;
  }
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      &lt;title&gt;Index&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt; 
          @using (Html.BeginForm())
          {
              @Html.ValidationSummary()
              &lt;p&gt;@Html.ValidationMessageFor(x =&gt; x.Name)&lt;/p&gt;
              &lt;p&gt;Your Full Name: @Html.TextBoxFor(x =&gt; x.Name)&lt;/p&gt;
              &lt;p&gt;@Html.ValidationMessageFor(x =&gt; x.Age)&lt;/p&gt;
              &lt;p&gt;Your current age: @Html.TextBoxFor(x =&gt; x.Age)&lt;/p&gt;
              &lt;p&gt;Your full address: 
              &lt;br /&gt;@Html.TextBoxFor(x =&gt; x.Street)&lt;/p&gt;
              @:City: @Html.TextBoxFor(x =&gt; x.City)
              @:State: @Html.TextBoxFor(x =&gt; x.State)
              @:Zip Code: @Html.TextBoxFor(x =&gt; x.Zip)
              &lt;input type="submit" value="Submit Form" /&gt;
          }
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How would I make it so that the validation for the the Street Address depends on other values/attributes in profile?</p>
  
  <p>If the user does not enter a Street, City, State, or ZIP, it should allow the user to submit the form. But if the User enters just a state but neither of the other 3, it should not allow it.</p>
  
  <p>It's either, they are all filled out, or none at all.</p>
  
  <p>How would I do this?</p>
  
  <p>After googling a bit, I tried [Compare()] but that did not work</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>public class HomeController : Controller
      {
          // GET: Home
          public ActionResult Index(Profile profile)
          {
              Profile _profile = profile ?? new Profile();
  
              if (string.IsNullOrEmpty(_profile.Street) || string.IsNullOrEmpty(_profile.State))
                  ModelState.AddModelError("Address", "All Address fields have to be empty, OR full");
  
              return View(_profile);
          }
  
  
      }
  </code></pre>
  
  <p>I think I figured it out, but I am not sure if this is the "right" way to do it?</p>