简体   繁体   中英

MVC Validation on Production Site Fails

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 "Sequence Contains no Elements . It points back to my Controller "model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single();".

Screenshot of the code causing the error

View

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

@Html.HiddenFor(m => m.UserId)
@Html.HiddenFor(m => m.AcceptSMS)
@Html.HiddenFor(m => m.IsActive)
@Html.HiddenFor(m => m.LastLoginDate)

@Html.HiddenFor(m => m.AddressId)

<div class="editor-label">
    Name
</div>
<div class="editor-field">
    @Html.EditorFor(m => m.FirstName)
    @Html.ValidationMessageFor(m => m.FirstName)
</div>

<div class="editor-field">
    @Html.EditorFor(m => m.LastName)
    @Html.ValidationMessageFor(m => m.LastName)
</div>

<div class="editor-label">
    @Html.LabelFor(m => m.Phone)
</div>
<div class="editor-field">
    @Html.TextBoxFor(m => m.Phone)
    @Html.ValidationMessageFor(m => m.Phone)
</div>

<div class="editor-label">
    @Html.LabelFor(m => m.Mobile)
</div>
<div class="editor-field">
    @Html.TextBoxFor(m => m.Mobile)
    @Html.ValidationMessageFor(m => m.Mobile)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DateOfBirth)
    @Html.ValidationMessageFor(model => model.DateOfBirth)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
    @Html.RadioButton("Gender", "male") male
    @Html.RadioButton("Gender", "female") female
    @Html.ValidationMessageFor(m => m.Gender)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.AddressId, "Address")
    @Html.ValidationMessageFor(m => m.AddressId)
</div>

Controller

[HttpPost]
    public ActionResult Edit(UserProfile model)
    {
         model.Address = db.Addresses.FirstOrDefault(a => 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 && 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 > 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);
    }

Model

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<int> AddressId { get; set; }

    [Required(ErrorMessage = "please select an activation status")]
    [Display(Name = "Is Active")]
    public bool IsActive { get; set; }
}

I'm guessing you're seeing error thrown here:

model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single();

Try .SingleOrDefault().

Also check if db.Addresses is not empty

if (model.Address != null && db.Addresses.Any())

EDIT: try this

model.Address = db.Addresses.FirstOrDefault(a => a.AddressId == model.AddressId);

EDIT2: only assign model.Address with existing address if it's not a new address:

if (model.AddressId != null && db.Addresses.Any(a => a.AddressId == model.AddressId))
    model.Address = db.Addresses.First(a => a.AddressId == model.AddressId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM