title: Date of birth custom range data annotation not working (breaking the page)
body: <p>I have a custom data annotation validator to validate the date of birth to be between 150 years from today. </p>
  
  <p>Here is my custom data annotation:</p>
  
  <pre><code>public class DateOfBirthRange : RangeAttribute
  {
      public DateOfBirthRange()
          : base(typeof(DateTime), DateTime.Now.AddYears(-150).ToShortDateString(), DateTime.Now.ToShortDateString()) { }
  }
  </code></pre>
  
  <p>Using it like this:</p>
  
  <pre><code>[Required(ErrorMessage = "BirthDate is required.")]
  [DisplayName("Birth Date")]
  [DateOfBirthRange(ErrorMessage = "BirthDate must be between {1:M/d/yyyy} and {2:M/d/yyyy}")]
  public DateTime BirthDate { get; set; }
  </code></pre>
  
  <p>This is a very strange problem because the error that I'm getting back has nothing to do with the data annotation. Its causing an error in my view here:</p>
  
  <pre><code>&lt;%: Html.DropDownListFor(m =&gt; m.JuniorSenior, (IEnumerable&lt;SelectListItem&gt;)ViewData["seniority"], new { @class = "input-small" })%&gt;
  </code></pre>
  
  <p><code><strong>ERROR:</strong> The ViewData item that has the key 'JuniorSenior' is of type 'System.String' but must be of type 'IEnumerable'.</code></p>
  
  <p>That error is strange because that part of the code is working perfectly fine. <strong>What also makes this strange is that this error only occurs when the date entered is before 150 years from today.</strong>  So that error only occurs when the date of birth validation fails. As I was debugging, I noticed that once I remove the custom data annotation, everything works fine and no error is encountered. </p>
  
  <p><code><strong>So that leads me to assume the problem is in my data annotation.</strong></code></p>
  
  <p>Here is my controller code in case you want to see what I'm trying to do.  </p>
  
  <pre><code>[HttpPost]
          public ActionResult Save(PatientModel patModel, FormCollection values)
          {
              // remove white space form text box fields
              patModel.FirstName = values["FirstName"].Trim();
              patModel.LastName = values["LastName"].Trim();
              patModel.Initials = values["Initials"].Trim();
              patModel.StreetAddress1 = values["StreetAddress1"].Trim();
              patModel.StreetAddress2 = values["StreetAddress2"].Trim();
              patModel.PostalCode = values["PostalCode"].Trim();
  
              if (ModelState.IsValid)
              {
                  try
                  {
                      // Pull the long form of the gender into the model.
                      if (!String.IsNullOrEmpty(values["genders"]))
                      {
                          patModel.Gender = values["genders"];
                      }
                      // Profile is valid, save it.
                      if (_Service.SaveProfile(Session["username"].ToString(), Session["password"].ToString(), patModel))
                          ViewData["SaveProfile"] = true;
                      else
                          ViewData["SaveProfile"] = false;
                  }
                  catch (Exception ex)
                  {
                      logger.Error("Function Name: Save  Message: " + ex.Message + "");
                  }
              }
              IntializeSelectLists(patModel);
              if (patModel.Title == "Select")
                  patModel.Title = "";
              if (patModel.JuniorSenior == "Select")
                  patModel.JuniorSenior = "";
              return View("Index", patModel);
          }
  </code></pre>
  
  <p>My IntializeSelectLists function:</p>
  
  <pre><code>public void IntializeSelectLists(PatientModel pm)
          {
              seniority = new[] { "Select", "Jr.", "Sr." };
              List&lt;SelectListItem&gt; JuniorSenior = new List&lt;SelectListItem&gt;();
              foreach (string item in seniority)
              {
                  SelectListItem alb = new SelectListItem { Text = item, Value = item };
                  JuniorSenior.Add(alb);
              }
              ViewData["seniority"] = JuniorSenior;
          }
  </code></pre>
  
  <p>Any help would be appreciated. </p>