// GET: /Expense/Edit public ActionResult Edit(int id) { var expense = expenseRepository.GetById(id); var viewModel = new ExpenseFormModel(expense); var categories = categoryRepository.GetAll(); viewModel.Category = categories.ToSelectListItems(expense.Category.CategoryId); return View(viewModel); }
public ActionResult Create() { var viewModel = new ExpenseFormModel(); var categories = categoryRepository.GetAll(); viewModel.Category = categories.ToSelectListItems(-1); viewModel.Date = DateTime.Today; return View(viewModel); }
public void Cannot_Create_Empty_Expense() { // Arrange ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object, expenseRepository.Object); // The MVC pipeline doesn't run, so binding and validation don't run. controller.ModelState.AddModelError("", "mock error message"); Mapper.CreateMap<ExpenseFormModel, CreateOrUpdateExpenseCommand>(); // Act ExpenseFormModel expense = new ExpenseFormModel(); expense.ExpenseId = 0; var result = controller.Save(expense) as ViewResult; // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.AreEqual("Create", result.ViewName); }
public ActionResult Save(ExpenseFormModel form) { if (ModelState.IsValid) { var command = Mapper.Map<ExpenseFormModel, CreateOrUpdateExpenseCommand>(form); var result = commandBus.Submit(command); if (result.Success) return RedirectToAction("Index"); } //if fail var categories = categoryRepository.GetAll(); form.Category = categories.ToSelectListItems(form.CategoryId); if(form.ExpenseId==0) return View("Create", form); else return View("Edit", form); }
public void Create_Expense_Redirects_To_Index() { // Arrange ExpenseController controller = new ExpenseController(commandBus.Object, categoryRepository.Object, expenseRepository.Object); commandBus.Setup(c => c.Submit(It.IsAny<CreateOrUpdateExpenseCommand>())).Returns(new CommandResult(true)); Mapper.CreateMap<ExpenseFormModel, CreateOrUpdateExpenseCommand>(); // Act ExpenseFormModel expense = new ExpenseFormModel { Transaction = "Mock Transaction", Date = DateTime.Now, Amount = 1000, CategoryId = 1 }; var result = controller.Save(expense) as RedirectToRouteResult; // Assert Assert.AreEqual("Index", result.RouteValues["action"]); }
public ActionResult Save(ExpenseFormModel form) { if (ModelState.IsValid) { var command = new CreateOrUpdateExpenseCommand { ExpenseId = form.ExpenseId, CategoryId = form.CategoryId, Date = form.Date, Transaction = form.Transaction, Amount = form.Amount }; var result = commandBus.Submit(command); if (result.Success) return RedirectToAction("Index"); } //if fail var categories = categoryRepository.GetAll(); form.Category = categories.ToSelectListItems(form.CategoryId); if(form.ExpenseId==0) return View("Create", form); else return View("Edit", form); }