// // GET: /Products/Create public ActionResult Create() { //ViewBag.Category = new SelectList( // db.Categories.ToList(), // "Category_ID", // "Category_Name") // ; //ViewBag.Gender = new SelectList( // db.Genders.ToList(), // "Gender_ID", // "Gender_Name" // ); //return View(); var model = new Products(); model.CategoryList = db.Categories.Select(x => new SelectListItem { Text = x.Category_Name, Value = x.Category_Name }).ToList(); model.GenderList = db.Genders.Select(x => new SelectListItem { Text = x.Gender_Name, Value = x.Gender_Name }).ToList(); return View(model); }
public ActionResult Create(Products products) { if (products.Image.ContentLength > (2 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "The Size of the Image is 2MB"); return View(); } if (!(products.Image.ContentType == "image/jpeg" || products.Image.ContentType == "image/gif")) { ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif"); return View(); } byte[] data = new byte[products.Image.ContentLength]; products.Image.InputStream.Read(data, 0, products.Image.ContentLength); products.Product_Photo = data; try { db.Products.Add(products); db.SaveChanges(); return RedirectToAction("Index"); } catch { var model = new Products(); model.CategoryList = db.Categories.Select(x => new SelectListItem { Text = x.Category_Name, Value = x.Category_Name }).ToList(); model.GenderList = db.Genders.Select(x => new SelectListItem { Text = x.Gender_Name, Value = x.Gender_Name }).ToList(); return View(products); } }
public ActionResult Edit(Products products) { if (ModelState.IsValid) { db.Entry(products).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(products); }