[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();
}
}
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = Id)]CustomerInfo customerinfo) { if (customerinfo.FirstName.Trim().Length == 0) ModelState.AddModelError(FirstName, First
Share
You should really step through and indicate where exactly it fails. Most likely this would tell you enough to fix the problem yourself. In particular, look at the line-number; that will take you to the line that is failing.
However, my guess is simply that one of
FirstName,LastName,PhoneorEmailisnull(which is the default for strings, so entirely expected) – or thatcustomerinfoitself is null.Changing to
(etc) will probably fix it for you.