Example #1
0
 public CategoryViewModel CreateCategory(NewCategoryViewModel model)
 {
     var category = Mapper.Map<Category>(model);
     context.Categories.Add(category);
     context.SaveChanges();
     return Mapper.Map<CategoryViewModel>(category);
 }
Example #2
0
        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);
        }