public ActionResult Create(Account account) { try { if (Session["UserAccountID"] == null) return RedirectToAction("Validate", "Login"); User user = (User)Session["User"]; ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"])); if (user.IsAdmin) ViewData["txtIsAdmin"] = "true"; else throw new Exception("You are not authorized to access this page."); if (ModelState.IsValid) { // Set NULLs to Empty Strings account = FillNulls(account); string validation = ValidateInput(account); if (!String.IsNullOrEmpty(validation)) { ViewData["ValidationMessage"] = validation; return View(account); } repository.CreateAccount(account); repository.CreateSampleData(account.AccountID); CommonMethods.CreateActivityLog((User) Session["User"], "Account", "Add", "Added account '" + account.AccountName + "' - ID: " + account.AccountID.ToString()); return RedirectToAction("Index"); } return View(account); } catch (Exception ex) { Helpers.SetupApplicationError("Account", "Create POST", ex.Message); return RedirectToAction("Index", "ApplicationError"); } }
private string ValidateInput(Account account) { if (String.IsNullOrEmpty(account.AccountName)) return "Account Name is required."; if (!String.IsNullOrEmpty(account.FTPServer) && !account.FTPServer.ToLower().StartsWith(@"ftp://")) return "FTP Server must start with ftp://"; return String.Empty; }
private Account FillNulls(Account account) { if (account.AccountDescription == null) account.AccountDescription = String.Empty; if (account.FTPServer == null) account.FTPServer = String.Empty; if (account.FTPUsername == null) account.FTPUsername = String.Empty; if (account.FTPPassword == null) account.FTPPassword = String.Empty; return account; }
private Account CreateNewAccount() { Account account = new Account(); account.AccountID = 0; account.AccountName = String.Empty; account.AccountDescription = String.Empty; account.FTPServer = String.Empty; account.FTPUsername = String.Empty; account.FTPPassword = String.Empty; account.IsActive = true; return account; }
public void UpdateAccount(Account account) { db.Entry(account).State = EntityState.Modified; db.SaveChanges(); }
public void CreateAccount(Account account) { db.Accounts.Add(account); db.SaveChanges(); }