繁体   English   中英

生产站点上的MVC验证失败

[英]MVC Validation on Production Site Fails

在Localhost上,我的用户帐单地址验证在提交之前有效。 一旦我将项目推向生产。 验证不会启动,并且我收到以下错误“序列不包含任何元素 。它指向我的控制器”。model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single(); ”。

导致错误的代码的屏幕截图

视图

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

我猜你在这里看到错误:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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