public async Task <IActionResult> Create([Bind("BudgetID,CategoryID,Description,Amount")] Expense expense) { if (ModelState.IsValid) { _context.Add(expense); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { budgetId = expense.BudgetID })); } return(View(expense)); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { var aspUser = new IdentityUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(aspUser, Input.Password); if (result.Succeeded) { var user = new User { FirstName = Input.FirstName, LastName = Input.LastName, Email = Input.Email, TotalExpense = 0 }; _context.Add(user); await _context.SaveChangesAsync(); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(aspUser); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = aspUser.Id, code = code }, protocol: Request.Scheme); var emailMessage = $"Welcome to ExpTrackr!<br><br>To finish the registration process, please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."; await _emailSender.SendEmailAsync(Input.Email, "Confirm Your Account", emailMessage); return(RedirectToPage("/Account/ThankYou")); } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> Create([Bind("CategoryName,UserID")] Category category) { var user = GetUser(); if (user.UserID != category.UserID) { return(NotFound()); } var viewModel = new CategoryViewModel { UserID = user.UserID, Categories = _context.Categories.Where(c => c.UserID == user.UserID) }; ViewData["DuplicateNameErrorMessage"] = ""; try { category.CategoryName = category.CategoryName.Trim(); } catch (NullReferenceException) { ViewData["DuplicateNameErrorMessage"] = "Category name cannot be empty"; return(View("Index", viewModel)); } var existingCategory = await _context.Categories .SingleOrDefaultAsync(c => c.CategoryName.ToLower() == category.CategoryName.ToLower() && c.UserID == category.UserID); if (existingCategory != null) { ViewData["DuplicateNameErrorMessage"] = "This category already exists"; return(View("Index", viewModel)); } if (ModelState.IsValid) { _context.Add(category); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View("Index", viewModel)); }
public async Task <IActionResult> Create([Bind("UserID,BudgetName,BudgetMax,BudgetTotal,CreationDate")] Budget budget) { var user = GetUser(); if (user.UserID != budget.UserID) { return(NotFound()); } ViewData["DuplicateNameErrorMessage"] = ""; try { budget.BudgetName = budget.BudgetName.Trim(); } catch (NullReferenceException) { ViewData["DuplicateNameErrorMessage"] = "Budget name cannot be empty"; return(View(budget)); } var existingBudget = await _context.Budgets .SingleOrDefaultAsync(b => b.BudgetName.ToLower() == budget.BudgetName.ToLower() && b.UserID == budget.UserID); if (existingBudget != null) { ViewData["DuplicateNameErrorMessage"] = "This budget already exists"; return(View(budget)); } if (ModelState.IsValid) { _context.Add(budget); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(budget)); }