public async Task <IActionResult> Edit(int Id, CategoryModel category) { var categorydetails = await _categoryRepository.getCategoryById(Id); if (categorydetails == null) { TempData["errormessage"] = "Invalid Category"; return(RedirectToAction("Index")); } var categoryNameExists = await _categoryRepository.getCategoryByName(category.Name); if (categoryNameExists != null && categoryNameExists.Id != Id) { var createCategoryModel = new CreateCategoryViewModel() { category = category, errormessage = "Category Name Already exists. Please try with other name" }; return(View(createCategoryModel)); } categorydetails.Name = category.Name; categorydetails.Slug = SlugGenerator.makeSlug(category.Name); await _categoryRepository.UpdateCategory(categorydetails); TempData["successmessage"] = "Category Updated Successfully"; return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(CategoryModel category) { var categoryNameExists = await _categoryRepository.getCategoryByName(category.Name); if (categoryNameExists != null) { var categorymodel = new CreateCategoryViewModel() { errormessage = "Category Name Already Present. Please select another Name" }; categorymodel.category = category; return(View(categorymodel)); } if (!ModelState.IsValid) { return(View()); } var categorydetails = new Category(); categorydetails.Name = category.Name; categorydetails.Slug = SlugGenerator.makeSlug(category.Name); categorydetails.UserId = (new LoggedInUser()).getUserId(); categorydetails.CreatedDate = new DateTime(); await _categoryRepository.AddCategory(categorydetails); TempData["successmessage"] = "Category Added Successfully"; return(RedirectToAction("Index")); }