public CategoryViewModel CreateCategory(NewCategoryViewModel model) { var category = Mapper.Map<Category>(model); context.Categories.Add(category); context.SaveChanges(); return Mapper.Map<CategoryViewModel>(category); }
public ActionResult Create(NewCategoryViewModel model) { var categories = categoryService.GetCategories(); ViewBag.Categories = new SelectList(categories, "Id", "Name"); if (ModelState.IsValid) { try { var categoryViewModel = categoryService.CreateCategory(model); Success(string.Format("Category [{0}] with id [{1}] created.", categoryViewModel.Name, categoryViewModel.Id)); return RedirectToAction("Index"); } catch (Exception e) { Error(e.Message); return View("Create", model); } } return View("Create", model); }