title: MVC Validation on Production Site Fails
body: <p>On Localhost my Validation for a user's billing address works, before its submitted. Once I pushed the project to production. Validation does not initiate and I receive the following error <a href="http://i.stack.imgur.com/90vUk.png" rel="nofollow">"Sequence Contains no Elements</a>. It points back to my Controller "model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single();".</p>
  
  <p><a href="http://i.stack.imgur.com/IQUWO.png" rel="nofollow">Screenshot of the code causing the error</a></p>
  
  <p>View</p>
  
  <pre><code>@using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  
  @Html.HiddenFor(m =&gt; m.UserId)
  @Html.HiddenFor(m =&gt; m.AcceptSMS)
  @Html.HiddenFor(m =&gt; m.IsActive)
  @Html.HiddenFor(m =&gt; m.LastLoginDate)
  
  @Html.HiddenFor(m =&gt; m.AddressId)
  
  &lt;div class="editor-label"&gt;
      Name
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(m =&gt; m.FirstName)
      @Html.ValidationMessageFor(m =&gt; m.FirstName)
  &lt;/div&gt;
  
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(m =&gt; m.LastName)
      @Html.ValidationMessageFor(m =&gt; m.LastName)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(m =&gt; m.Phone)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.TextBoxFor(m =&gt; m.Phone)
      @Html.ValidationMessageFor(m =&gt; m.Phone)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(m =&gt; m.Mobile)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.TextBoxFor(m =&gt; m.Mobile)
      @Html.ValidationMessageFor(m =&gt; m.Mobile)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.DateOfBirth)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.DateOfBirth)
      @Html.ValidationMessageFor(model =&gt; model.DateOfBirth)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.Gender)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.RadioButton("Gender", "male") male
      @Html.RadioButton("Gender", "female") female
      @Html.ValidationMessageFor(m =&gt; m.Gender)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.AddressId, "Address")
      @Html.ValidationMessageFor(m =&gt; m.AddressId)
  &lt;/div&gt;
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>[HttpPost]
      public ActionResult Edit(UserProfile model)
      {
           model.Address = db.Addresses.FirstOrDefault(a =&gt; a.AddressId == model.AddressId);
  
          // check to verify that the phone number length is correct
          // (when using an input mask, this shouldn't be a problem)
          if (model.Phone.Length != 14)
          {
              ModelState.AddModelError("", "please enter a valid phone number");
          }
  
          if (model.Mobile != null &amp;&amp; model.Mobile.Length != 14)
          {
              ModelState.AddModelError("", "please enter a valid mobile number");
          }
  
          // checks to make sure the user is 18 years old
          if (model.DateOfBirth.Date &gt; DateTime.Now.AddHours(2).Date.AddYears(-18))
          {
              ModelState.AddModelError("", "you must be at least 18 years of age");
          }
  
          if (ModelState.IsValid)
          {
              db.Entry(model).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("MyAccount", "Account");
          }
  
          return View(model);
      }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>namespace XYZ.Models.Domain
  {public class UserProfileMetadata
  {
      [HiddenInput(DisplayValue = false)]
      [Display(Name = "User Id")]
      public int UserId { get; set; }
  
      [Required(ErrorMessage = "please enter your first name")]
      [Display(Name = "First name")]
      public string FirstName { get; set; }
  
      [Required(ErrorMessage = "please enter your last name")]
      [Display(Name = "Last name")]
      public string LastName { get; set; }
  
      [Display(Name = "Date of Birth")]
      [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
      public DateTime DateOfBirth { get; set; }
  
      [Required(ErrorMessage = "please enter a phone number")]
      public string Phone { get; set; }
  
      //[Required(ErrorMessage = "please enter a mobile number")]
      public string Mobile { get; set; }
  
      [Required(ErrorMessage = "please select a gender")]
      public string Gender { get; set; }
  
      [Display(Name = "Address")]
      public Nullable&lt;int&gt; AddressId { get; set; }
  
      [Required(ErrorMessage = "please select an activation status")]
      [Display(Name = "Is Active")]
      public bool IsActive { get; set; }
  }
  </code></pre>