public void Create(Categories item) { if (String.IsNullOrEmpty(item.Name)) { throw new ArgumentException("Name can not be null!"); } item.Id = this.getMaxId() + 1; dbShopContext.Categories.Add(item); dbShopContext.SaveChanges(); }
public ActionResult Create([Bind(Include = "ProductName,BrandName,Price,Units,Discount,SupplierId,SubCategoryId")] Product product, HttpPostedFileBase Proimage) { ViewBag.SubCategory = db.SubCategories.ToList(); ViewBag.Supplier = db.Suppliers.ToList(); if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); TempData["status"] = "success"; TempData["message"] = product.ProductName + " added"; if (Proimage != null) { if (Proimage.ContentLength > 0) { if (Path.GetExtension(Proimage.FileName).ToLower() == ".jpg" || Path.GetExtension(Proimage.FileName).ToLower() == ".jpeg" || Path.GetExtension(Proimage.FileName).ToLower() == ".png") { var path = Path.Combine(Server.MapPath("~/Images/"), product.ProductId + ".jpg"); Proimage.SaveAs(path); } } } return(RedirectToAction("Index")); } else { ViewBag.status = "danger"; ViewBag.message = "Please fill all fields and try again"; } return(View(product)); }