public Client createClient(ClientCreateForHttpPost cp) { var c = new Client(); c.Id = dc.Clients.Max(n => n.Id) + 1; c.FirstName = cp.FirstName; c.LastName = cp.LastName; c.AccountNumber = cp.AccountNumber; c.Banks = new List<Bank>(); foreach (var item in cp.BankIds) { Bank b = dc.Banks.FirstOrDefault(bk => bk.Id == item); if (b != null) c.Banks.Add(b); } dc.Clients.Add(c); dc.SaveChanges(); return getClient(c.Id); }
public ActionResult Create(ClientCreateForHttpPost newItem, FormCollection form) { if (ModelState.IsValid) { try { var createdClient = repo.createClient(newItem); clientToCreate.Clear(); if (createdClient == null) { return View("Error", vme.GetErrorModel(ModelState)); } else { return RedirectToAction("Details", new { Id = createdClient.Id }); } } catch (System.Exception ex) { ModelState.AddModelError("Exception Message", ex.Message); clientToCreate.Clear(); return View("Error", vme.GetErrorModel(ModelState)); } } // No. So report and send back to user to re-enter else { var errorMessage = "Client should have at least one Bank"; if (newItem.BankIds == null) ModelState.AddModelError("BankSelectList", errorMessage); clientToCreate.FirstName = newItem.FirstName; clientToCreate.LastName = newItem.LastName; clientToCreate.AccountNumber = newItem.AccountNumber; return View(clientToCreate); } }
public ActionResult Edit(int id, ClientCreateForHttpPost newItem) { if (ModelState.IsValid) { var result = repo.getClient(id); // Add update logic here repo.editClient(id, newItem); return RedirectToAction("Index"); } else { return View("Error"); } }
public Client editClient(int id, ClientCreateForHttpPost newitem) { var stu = dc.Clients.FirstOrDefault(n => n.Id == id); stu.FirstName = newitem.FirstName; stu.LastName = newitem.LastName; stu.AccountNumber = Convert.ToInt32(newitem.AccountNumber); stu.Banks = new List<Bank>(); foreach (var item in newitem.BankIds) { Bank c = dc.Banks.FirstOrDefault(bk => bk.Id == item); if (c != null) stu.Banks.Add(c); } dc.SaveChanges(); return stu; }