[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude = "Id")]CustomerInfo customerinfo)
{
if (customerinfo.FirstName.Trim().Length == 0)
ModelState.AddModelError("FirstName", "First name is required.");
if (customerinfo.LastName.Trim().Length == 0)
ModelState.AddModelError("LastName", "Last name is required.");
if (customerinfo.Phone.Length > 0 && !Regex.IsMatch(customerinfo.Phone, @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"))
ModelState.AddModelError("Phone", "Invalid phone number.");
if (customerinfo.Email.Length > 0 && !Regex.IsMatch(customerinfo.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
ModelState.AddModelError("Email", "Invalid email address.");
if (!ModelState.IsValid)
return View();
try
{
BLL.Customer customer = new BLL.Customer();
customer.CreateCustomer(customerinfo);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
答案 0 :(得分:1)
你应该直接指出它确切失败的地方。很可能这会告诉你足够自己解决问题。特别是,看一下行号;这将带你到失败的那一行。
但是,我的猜测只是FirstName
,LastName
,Phone
或Email
中的一个是null
(这是字符串的默认值,所以完全预期的) - 或customerinfo
本身为空。
更改为
if (customerinfo.FirstName == null || customerinfo.FirstName.Trim().Length == 0)
ModelState.AddModelError("FirstName", "First name is required.");
(等)可能会为你修复它。