public async Task <IActionResult> Add([Bind("Name,Amount,Variable,Amount,WorkHours,Function,Company,StartDate")] IncomesAddVm incomeVm) { if (ModelState.IsValid) { string data = HttpContext.Session.GetString("User"); UserSessionModel userSession = JsonConvert.DeserializeObject <UserSessionModel>(data); var fk = await _context.tbl_users.FirstOrDefaultAsync(u => u.Id == userSession.Id); Income newIncomes = new Income { FK = fk, Name = incomeVm.Name, Amount = incomeVm.Amount, WorkHours = incomeVm.WorkHours, Variable = incomeVm.Variable, Function = incomeVm.Function, Company = incomeVm.Company, StartDate = incomeVm.StartDate }; _context.Add(newIncomes); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(incomeVm)); }
// GET: Incomes/Add public IActionResult Add() { if (IsLoggedIn()) { IncomesAddVm incomesAddVm = new IncomesAddVm(); return(View(incomesAddVm)); } return(RedirectToAction("Login", "User")); }
// GET: Incomes/Edit/5 public async Task <IActionResult> Edit(long?id) { if (IsLoggedIn()) { Income currentIncomes = await _context.tbl_incomes.Where(e => e.Id == id).FirstAsync(); IncomesAddVm incomeAddVm = new IncomesAddVm { Name = currentIncomes.Name, Amount = currentIncomes.Amount, WorkHours = currentIncomes.WorkHours, Variable = currentIncomes.Variable, Function = currentIncomes.Function, Company = currentIncomes.Company, StartDate = currentIncomes.StartDate }; return(View(incomeAddVm)); } return(RedirectToAction("Login", "User")); }
public async Task <IActionResult> Edit(long id, [Bind("Name,Amount,Variable,Amount,WorkHours,Function,Company,StartDate")] IncomesAddVm incomeVm) { if (ModelState.IsValid) { Income updatedIncome = await _context.tbl_incomes.Where(e => e.Id == id).FirstAsync(); // Copy over properties updatedIncome.Name = incomeVm.Name; updatedIncome.Variable = incomeVm.Variable; updatedIncome.Amount = incomeVm.Amount; updatedIncome.WorkHours = incomeVm.WorkHours; updatedIncome.Function = incomeVm.Function; updatedIncome.Company = incomeVm.Company; updatedIncome.StartDate = incomeVm.StartDate; try { _context.Update(updatedIncome); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IncomesExists(updatedIncome.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(incomeVm)); }