public JsonResult Update(subCategoryViewModal 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.SubCategories.SingleOrDefault(c => c.Id == id);
                if (catInDB != null)
                {
                    catInDB.name       = model.name;
                    catInDB.CategoryId = model.CategoryId;

                    _context.SaveChanges();
                    var cat_new = _context.SubCategories.SingleOrDefault(c => c.Id == id);

                    return(Json(new { cat = cat_new, success = true, responseText = "Sub Category " + model.name + " has been successfuly Updated!" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { success = false, responseText = "Sub Category " + model.name + " doesnot exists!" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (JsonException jx)
            {
                throw new JsonException("Unable to Update Sub Category", jx);
            }
        }
Esempio n. 2
0
        public ActionResult SubCategory()
        {
            try
            {
                var categories = _context.Categories.ToList();

                var viewModel = new subCategoryViewModal
                {
                    categories = categories
                };

                return(View(viewModel));
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to load categories,brands and products", ex);
            }
        }
        public JsonResult Add(subCategoryViewModal model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet));
                }

                bool _exists = _context.SubCategories.Any(c => c.name == model.name);

                if (!_exists)
                {
                    SubCategory cat = new SubCategory();
                    cat.name       = model.name;
                    cat.CategoryId = model.CategoryId;
                    cat.date       = DateTime.Now;

                    _context.SubCategories.Add(cat);
                    _context.SaveChanges();

                    var cat_new = _context.SubCategories.Include(c => c.Category).SingleOrDefault(c => c.Id == cat.Id);



                    return(Json(new { data = cat_new, success = true, responseText = "Sub Category " + model.name + " has been successfuly added!" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { success = false, responseText = "Sub Category " + model.name + " already exists!" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (JsonException jx)
            {
                throw new JsonException("Unable to add Sub Category", jx);
            }
        }