public ActionResult Product(int id) { try { var prod = _context.Products.Where(it => it.Id == id).SingleOrDefault(); ViewBag.Message = "Your Single item page."; var viewModel = new productViewModal { Id = prod.Id, name = prod.name, price = prod.price, whileSalePrice = prod.whileSalePrice, color = prod.color, quantity = prod.quantity, car = prod.model, details = prod.details, status = prod.status, imageurl = prod.image_url }; return(View(viewModel)); } catch (Exception ex) { throw new Exception("Sorry there was error loading the item", ex); } }
public JsonResult Add(productViewModal model) { try { if (!ModelState.IsValid) { return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet)); } bool _exists = _context.Products.Any(c => c.name == model.name); if (!_exists) { Product product = new Product(); product.BrandId = model.BrandId; product.color = model.color; product.date = DateTime.Now; product.details = model.details; var filename = Path.GetFileName(model.image.FileName); model.image.SaveAs(Server.MapPath("../Images/product/" + filename)); product.image_url = "/Images/product/" + filename; product.model = model.car; product.name = model.name; product.price = model.price; product.priority = model.price; product.quantity = model.quantity; product.status = model.status; product.whileSalePrice = model.whileSalePrice; _context.Products.Add(product); _context.SaveChanges(); var product_new = _context.Products.Include(c => c.Brand).SingleOrDefault(c => c.Id == product.Id); return(Json(new { data = product_new, success = true, responseText = "Product " + model.name + " has been successfuly added!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, responseText = "Product " + model.name + " already exists!" }, JsonRequestBehavior.AllowGet)); } } catch (JsonException jx) { throw new JsonException("Unable to add Product", jx); } }
public ActionResult Products() { try { var brands = _context.Brands.ToList(); var viewModel = new productViewModal { brand = brands }; return(View(viewModel)); } catch (Exception ex) { throw new Exception("Unable to load categories,brands and products", ex); } }
public JsonResult Update(productViewModal model, int id) { try { if (!ModelState.IsValid) { return(Json(new { success = false, responseText = "Sorry! There was error perfoming your action." }, JsonRequestBehavior.AllowGet)); } var productInDB = _context.Products.Include(pr => pr.Brand).SingleOrDefault(c => c.Id == id); if (productInDB != null) { productInDB.BrandId = model.BrandId; productInDB.color = model.color; productInDB.date = DateTime.Now; productInDB.details = model.details; var filename = Path.GetFileName(model.image.FileName); model.image.SaveAs(Server.MapPath("../../Images/product" + filename)); productInDB.image_url = "/Images/product/" + filename; productInDB.model = model.car; productInDB.name = model.name; productInDB.price = model.price; productInDB.priority = model.price; productInDB.quantity = model.quantity; productInDB.status = model.status; productInDB.whileSalePrice = model.whileSalePrice; _context.SaveChanges(); var cat_new = _context.Products.SingleOrDefault(c => c.Id == id); return(Json(new { cat = cat_new, success = true, responseText = "Product " + model.name + " has been successfuly Updated!" }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { success = false, responseText = "Product " + model.name + " doesnot exists!" }, JsonRequestBehavior.AllowGet)); } } catch (JsonException jx) { throw new JsonException("Unable to Update Product", jx); } }