public ActionResult add(Contacts contact) { bool result = true; try { if (contact.name == null) { ModelState.AddModelError("name", "*This field can't be null."); TempData["ModelState"] = ModelState; result = false; } if (contact.eMail == null) { ModelState.AddModelError("eMail", "*This field can't be null."); TempData["ModelState"] = ModelState; result = false; } if (contact.country == null) { ModelState.AddModelError("country", "*This field can't be null."); TempData["ModelState"] = ModelState; result = false; } if (contact.city == null) { ModelState.AddModelError("city", "*This field can't be null."); TempData["ModelState"] = ModelState; result = false; } if (contact.phone == null) { ModelState.AddModelError("phone", "*This field can't be null."); TempData["ModelState"] = ModelState; result = false; } if (result) { ModelState.AddModelError("contact", "Thanks to be in contact with us."); TempData["ModelState"] = ModelState; contactServices.addContact(contact); } } catch (Exception) { } return View("index"); }
public void addTest() { ContactController target = new ContactController(iCommentServices.Object); Contacts contact = new Contacts() ; contact.city = "City"; contact.country = "Country"; contact.eMail = "email"; contact.id = 10; contact.name = "name"; contact.phone = null; ViewResult actual = target.add(contact) as ViewResult; TempDataDictionary tempData = actual.TempData as TempDataDictionary; ModelStateDictionary modelState = tempData["ModelState"] as ModelStateDictionary; Assert.AreEqual(actual.ViewName, "index"); Assert.IsTrue(modelState.ContainsKey("phone")); contact.name = null; actual = target.add(contact) as ViewResult; tempData = actual.TempData as TempDataDictionary; modelState = tempData["ModelState"] as ModelStateDictionary; Assert.AreEqual(actual.ViewName, "index"); Assert.AreEqual(2,modelState.Count); }
public void addContact(Contacts contact) { db.contacts.Add(contact); db.SaveChanges(); }