public ActionResult Create(ProductViewModel model) { try { G2BPMDataContext context = new G2BPMDataContext(); Product product = new Product() { Title = model.Title, ShortDesc = model.ShortDesc, FullDesc = model.FullDesc }; // Check image is uploaded or not. // If uploaded, add/change image. // If not, leave old image there, no change. if (model.ImageUrl != null) { product.ImageUrl = model.ImageUrl; } context.Products.InsertOnSubmit(product); context.SubmitChanges(); // Auto create test product //AutoCreateProduct(); return(Json(new { success = true })); } catch (Exception e) { return(Json(new { success = false, error = e.Message })); } }
public ActionResult Edit(ProductViewModel model) { try { G2BPMDataContext context = new G2BPMDataContext(); Product product = context.Products.FirstOrDefault(i => i.Id == model.Id); product.Title = model.Title; product.ShortDesc = model.ShortDesc; product.FullDesc = model.FullDesc; // Check image is uploaded or not. // If uploaded, add/change image. // If not, leave old image there, no change. if (model.ImageUrl != null) { product.ImageUrl = model.ImageUrl; } context.SubmitChanges(); return(Json(new { success = true })); } catch (Exception e) { return(Json(new { success = false, error = e.Message })); } }
private void AutoCreateProduct() { G2BPMDataContext context = new G2BPMDataContext(); for (int i = 1; i <= 24; i++) { Product s = new Product() { Title = "auto-create-" + i, ImageUrl = "", ShortDesc = "auto-create-product", FullDesc = "auto-create-product-auto-create-product-auto-create-product-auto-create-product-auto-create-product" }; context.Products.InsertOnSubmit(s); } context.SubmitChanges(); }
public ActionResult Delete(int id) { try { G2BPMDataContext context = new G2BPMDataContext(); Product product = context.Products.FirstOrDefault(i => i.Id == id); context.Products.DeleteOnSubmit(product); context.SubmitChanges(); TempData["ErrorMessageOfDelete"] = ""; return(RedirectToAction("ListAll")); } catch (Exception e) { TempData["ErrorMessageOfDelete"] = "Couldn't deleted the product. Error: " + e.Message; return(RedirectToAction("ListAll")); } }