生产站点上的MVC验证失败

时间:2016-01-15 21:42:04

标签: c# asp.net-mvc validation model-view-controller controller

在Localhost上,我对用户的帐单邮寄地址的验证在提交之前有效。一旦我将项目推向生产。验证未启动,我收到以下错误"Sequence Contains no Elements。它指向我的Controller“model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single();”。

Screenshot of the code causing the error

查看

@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>

控制器

[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);
    }

模型

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; }
}

1 个答案:

答案 0 :(得分:0)

我猜你在这里看到错误:

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

尝试.SingleOrDefault()。

同时检查db.Addresses是否为空

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

编辑:试试这个

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

EDIT2:如果它不是新地址,则只分配带有现有地址的model.Address:

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