【发布时间】:2016-04-21 14:15:32
【问题描述】:
在本地主机上,我对用户帐单地址的验证在提交之前有效。一旦我将项目推向生产。验证未启动,我收到以下错误 "Sequence Contains no Elements。它指向我的控制器“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; }
}
【问题讨论】:
-
model.Address = db.Addresses.Where(a => a.AddressId == model.AddressId).Single();可能会引发错误 - 如果有可能没有一个匹配项,您应该使用.FirstOrDefault() -
它适用于您的本地主机而不是生产环境的事实让我认为这可能是一个部署问题。您能否验证您的所有文件是否正确上传?我想在部署过程中可能遗漏了一个 javascript 文件。
-
而且您显示的代码永远不会保存任何内容。您的
ModelState.AddModelError("", "please enter a billing address");添加了ModelState错误,因此if (ModelState.IsValid)始终返回 false 并且始终返回视图。 -
感谢您抽出时间回答我的问题。 @Chad 我登录到主机文件管理器,所有文件都已推送。清除缓存,并包含 Stephen Muecke 建议的更改 ".FirstOrDefault()" 。还是没有成功。
标签: c# asp.net-mvc validation model-view-controller controller