public ActionResult Create([Bind(Include = "CardId,Active,VisitorName")] VisitorCard visitorCard) { if (ModelState.IsValid) { if (string.IsNullOrEmpty(visitorCard.VisitorName) || string.IsNullOrEmpty(visitorCard.VisitorName.Trim())) { ModelState.AddModelError(nameof(visitorCard.VisitorName), "Visitor name is required."); return(View(visitorCard)); } visitorCard.VisitorName = visitorCard.VisitorName.Trim(); if (db.VisitorCard .Any( vc => visitorCard.Active && vc.Active && string.Compare( vc.VisitorName, visitorCard.VisitorName, StringComparison.InvariantCultureIgnoreCase) == 0) ) { ModelState.AddModelError(nameof(visitorCard.VisitorName), "Visitor with this name already has a card"); return(View(visitorCard)); } db.VisitorCard.Add(visitorCard); db.SaveChanges(); return(View("Details", visitorCard)); } return(View(visitorCard)); }
public void Should_Create_CreateCard() { var vc = new VisitorCard() { VisitorName = "Name_" + Guid.NewGuid(), Active = true, }; ViewResult result = controller.Create(vc) as ViewResult; Assert.IsNotNull(result); }
// GET: VisitorCards/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } VisitorCard visitorCard = db.VisitorCard.Find(id); if (visitorCard == null) { return(HttpNotFound()); } return(View(visitorCard)); }
void Should_Create_FailCreateDuplicate() { var vc = new VisitorCard() { VisitorName = "Name_" + Guid.NewGuid(), Active = true, }; controller.Create(vc); var vc2 = new VisitorCard() { VisitorName = vc.VisitorName, Active = true, }; controller.ViewData.ModelState.Clear(); controller.Create(vc2); Assert.IsFalse(controller.ViewData.ModelState.IsValid); }