public ActionResult Create(CreditCardAndAccountViewModel creditCardAndAccount) { if (db.creditcard.Where(x => x.cardNumber == creditCardAndAccount.creditcard.cardNumber).Count() > 0) { ModelState.AddModelError("creditcard.cardNumber", "Credit card number all ready exists."); } if (ModelState.IsValid) { creditCardAndAccount.account.creditcard.Add(creditCardAndAccount.creditcard); db.account.Add(creditCardAndAccount.account); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(creditCardAndAccount)); }
public ActionResult Create(CreditCardAndAccountViewModel creditCardAndAccount) { if (db.creditcard.Where(x => x.cardNumber == creditCardAndAccount.creditcard.cardNumber).Count() > 0) { ModelState.AddModelError("creditcard.cardNumber", "Credit card number all ready exists."); } if (creditCardAndAccount.existing) { foreach (var key in ModelState.Keys.Where(x => x.StartsWith("account."))) { ModelState[key].Errors.Clear(); } } if (ModelState.IsValid) { if (creditCardAndAccount.existing) { db.creditcard.Add(creditCardAndAccount.creditcard); } else { creditCardAndAccount.account.creditcard.Add(creditCardAndAccount.creditcard); db.account.Add(creditCardAndAccount.account); } db.SaveChanges(); return(RedirectToAction("Index")); } GenerateCreditCard creditCardGenerator = new GenerateCreditCard(db); ViewBag.creditcard = new { accountID = new SelectList((from acct in db.account select new { accountID = acct.accountID, fullname = acct.accountFirstName + " " + acct.accountLastName }), "accountID", "fullname", creditCardAndAccount.creditcard.accountID) }; return(View(creditCardAndAccount)); }