public JsonResult Update(categoryViewModal model, int id) { try { if (!ModelState.IsValid) { return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet)); } var catInDB = _context.Categories.SingleOrDefault(c => c.Id == id); if (catInDB != null) { catInDB.name = model.name; _context.SaveChanges(); var cat_new = _context.Categories.SingleOrDefault(c => c.Id == id); return(Json(new { cat = cat_new, success = true, responseText = "Category " + model.name + " has been successfuly Updated!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, responseText = "Category " + model.name + " doesnot exists!" }, JsonRequestBehavior.AllowGet)); } } catch (JsonException jx) { throw new JsonException("Unable to Update Category", jx); } }
public JsonResult Add(categoryViewModal model) { try { if (!ModelState.IsValid) { return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet)); } bool _exists = _context.Categories.Any(c => c.name == model.name); if (!_exists) { Category cat = new Category(); cat.name = model.name; cat.date = DateTime.Now; _context.Categories.Add(cat); _context.SaveChanges(); var cat_new = _context.Categories.SingleOrDefault(c => c.Id == cat.Id); return(Json(new { data = cat_new, success = true, responseText = "Category " + model.name + " has been successfuly added!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, responseText = "Category " + model.name + " already exists!" }, JsonRequestBehavior.AllowGet)); } } catch (JsonException jx) { throw new JsonException("Unable to add Category", jx); } }