这是我第一次尝试使用数据注释,并且在尝试测试验证时仍然收到上述错误。
我的模型看起来像:
public class SupplierValidationModel
{
[Display(Name = "Company Name:")]
[Required(ErrorMessage = "Company Name is Required")]
public string Name { get; set; }
[Display(Name = "Company Address:")]
[Required(ErrorMessage = "Company Address is Required")]
public string Address { get; set; }
[Display(Name = "Company City:")]
[Required(ErrorMessage = "Company City is Required")]
public string City { get; set; }
[Display(Name = "Company State:")]
[Required(ErrorMessage = "Company State is Required")]
public string State { get; set; }
[Display(Name = "Company Zip:")]
[Required(ErrorMessage = "Company Zip is Required")]
public string Zip { get; set; }
[Display(Name = "Company Phone:")]
[Required(ErrorMessage = "Company Phone is Required")]
[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "A Valid Phone Number is Required")]
public string Phone { get; set; }
[Display(Name = "Contact Name:")]
[Required(ErrorMessage = "Contact Name is Required")]
public string ContactName { get; set; }
[Display(Name = "Contact E-Mail:")]
[Required]
[EmailAddress(ErrorMessage = "Valid E-Mail Required")]
public string ContactEmail { get; set; }
[Display(Name = "Contact Phone:")]
[Required]
[RegularExpression(@"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}", ErrorMessage = "A Valid Phone Number is Required")]
public string ContactPhone { get; set; }
[Display(Name = "Added By:")]
[Required(ErrorMessage = "Added By Must Be Entered")]
public string AddedBy { get; set; }
}
我的观点看起来像这样;
@model SupplierValidationModel
@using (Html.BeginForm("InsertSupplier", "Admin", FormMethod.Post))
{
@Html.ValidationSummary(false)
<table>
<tr>
<td>@Html.LabelFor(m => m.Name)</td>
<td>@Html.TextBoxFor(m => m.Name) @Html.ValidationMessageFor(m=>m.Name)</td>
<td>@Html.LabelFor(m => m.Address)</td>
<td>@Html.TextBoxFor(m => m.Address) @Html.ValidationMessageFor(m => m.Address)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.City)</td>
<td>@Html.TextBoxFor(m => m.City) @Html.ValidationMessageFor(m => m.City)</td>
<td>@Html.LabelFor(m => m.State)</td>
<td>@Html.DropDownListFor(m => m.State, StateList.States) @Html.ValidationMessageFor(m => m.State)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.Zip)</td>
<td>@Html.TextBoxFor(m => m.Zip) @Html.ValidationMessageFor(m => m.Zip)</td>
<td>@Html.LabelFor(m => m.Phone)</td>
<td>@Html.TextBoxFor(m => m.Phone) @Html.ValidationMessageFor(m => m.Phone)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.ContactName)</td>
<td>@Html.TextBoxFor(m => m.ContactName) @Html.ValidationMessageFor(m => m.ContactName)</td>
<td>@Html.LabelFor(m => m.ContactEmail)</td>
<td>@Html.TextBoxFor(m => m.ContactEmail) @Html.ValidationMessageFor(m => m.ContactEmail)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.ContactPhone)</td>
<td>@Html.TextBoxFor(m => m.ContactPhone) @Html.ValidationMessageFor(m => m.ContactPhone)</td>
<td>@Html.LabelFor(m => m.AddedBy)</td>
<td>@Html.TextBoxFor(m => m.AddedBy) @Html.ValidationMessageFor(m => m.AddedBy)</td>
</tr>
<tr>
<td colspan="4" align="right"><input type="submit" value="Add Supplier" /></td>
</tr>
</table>
<input type="hidden" name="userId" value="@ViewBag.Employee.AccountName" />
}
最后我的行动是;
[HttpPost]
public ActionResult InsertSupplier(SupplierValidationModel model)
{
// string userName = coll["userId"].ToString();
if (ModelState.IsValid)
{
DataOps.InsertSupplier(model);
return View();
}else{
ViewData["Valid"] = false;
//return RedirectToAction("SupplierInfo", "Admin", new {employeeAcct = userName });
return View(model);
}
}
无论什么时候我再次返回View()并且只想用相关的错误消息加载表单,我得到的是无法执行运行时错误。
我已经完成了很多教程,并且所有内容都在ModelState中正确加载,但它没有效,但从不渲染表单。
答案 0 :(得分:1)
您隐藏字段中的@ ViewBag.Employee可能为null。如果你在GET操作中加载了它,你也必须在POST上加载。