コード例 #1
0
        public JsonResult Update(brandViewModal model, int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet));
                }

                var brandInDB = _context.Brands.SingleOrDefault(c => c.Id == id);
                if (brandInDB != null)
                {
                    brandInDB.name          = model.name;
                    brandInDB.SubCategoryId = model.SubCategoryId;

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

                    return(Json(new { cat = cat_new, success = true, responseText = "Brand " + model.name + " has been successfuly Updated!" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { success = false, responseText = "Brand " + model.name + " doesnot exists!" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (JsonException jx)
            {
                throw new JsonException("Unable to Update Brand", jx);
            }
        }
コード例 #2
0
        public ActionResult Brands()
        {
            try
            {
                var subCategories = _context.SubCategories.ToList();

                var viewModel = new brandViewModal
                {
                    subcategories = subCategories
                };

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

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

                if (!_exists)
                {
                    Brand brand = new Brand();
                    brand.name          = model.name;
                    brand.SubCategoryId = model.SubCategoryId;
                    brand.date          = DateTime.Now;

                    _context.Brands.Add(brand);
                    _context.SaveChanges();

                    var brand_new = _context.Brands.Include(c => c.SubCategory).SingleOrDefault(c => c.Id == brand.Id);



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