public async Task <IActionResult> Create([Bind("Id,Title,ExpenseCategoryId,PaymentTypeId,Amount")] Expense expense)
        {
            if (ModelState.IsValid)
            {
                _context.Add(expense);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ExpenseCategoryId"] = new SelectList(_context.ExpenseCategories, "Id", "Id", expense.ExpenseCategoryId);
            ViewData["PaymentTypeId"]     = new SelectList(_context.PaymentTypes, "Id", "Id", expense.PaymentTypeId);
            return(View(expense));
        }
Example #2
0
        public async Task <IActionResult> Create(ExpenseCreateViewModel cvm)
        {
            Expense newExpense = new Expense()
            {
                Amount        = cvm.Amount,
                CategoryId    = cvm.CategoryId,
                Description   = cvm.Description,
                Date          = cvm.Date,
                PhotoUrl      = cvm.PhotoUrl,
                ExpenseUserId = User.FindFirstValue(ClaimTypes.NameIdentifier)
            };

            newExpense.Category = await _dbContext.Categories.FirstOrDefaultAsync(x => x.Id == newExpense.CategoryId);

            if (String.IsNullOrEmpty(newExpense.PhotoUrl))
            {
                _photoService.AssignPicToExpense(newExpense);
            }

            _dbContext.Add(newExpense);
            await _dbContext.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }