// GET Account/Form/{id} public ActionResult Form(int? id) { if (id != null) { Account account = db.Accounts.Find(id); account.Contacts.Add(new Contact { }); return View(account); } else { var account = new Account { }; return View(account); } //Never happen return View(); }
public ActionResult Form(Account account) { if (ModelState.IsValid) { Account oldAccount = db.Accounts.Find(account.ID); if(oldAccount != null) { oldAccount.Name = account.Name; for (int i = 0; i < oldAccount.Contacts.Count; i++) { oldAccount.Contacts[i].FirstName = account.Contacts[i].FirstName; oldAccount.Contacts[i].LastName = account.Contacts[i].LastName; oldAccount.Contacts[i].Email = account.Contacts[i].Email; } if ( (oldAccount.Contacts.Count < account.Contacts.Count) ) // && LASTNAME != NULL { oldAccount.Contacts.Add(new Contact { FirstName = account.Contacts.Last().FirstName, LastName = account.Contacts.Last().LastName, Email = account.Contacts.Last().Email }); } db.SaveChanges(); return RedirectToAction("List"); } db.Accounts.Add(account); db.SaveChanges(); return RedirectToAction("List"); } return View(account); }