public PartialViewResult AddProductPVAJAX(string pName, string pDesc, string pStock) { demo_appEntities1 DB = new demo_appEntities1(); List <SImpleWebApp.Models.T_products> plist = new List <SImpleWebApp.Models.T_products>(); var query = from st in DB.T_products where st.P_name.Contains("") select st; plist = query.ToList(); if (pName == "" || pDesc == "" || pStock == null) { Response.Write("Fields cannot be empty, please fill in all detils"); return(PartialView(plist)); } T_products newProduct = new T_products(); newProduct.P_name = pName; newProduct.P_description = pDesc; try { newProduct.P_stock = int.Parse(pStock); } catch (Exception e) { throw e; } if (DB.T_products.Find(newProduct.P_id) != null) { return(PartialView(plist)); } DB.T_products.Add(newProduct); int res; try { res = DB.SaveChanges(); } catch (Exception e) { throw e; } if (res > 0) { Response.Write("new product was added successfuly"); } else { Response.Write("Operation Failed"); } return(PartialView("SearchProductAJAX", plist)); }
public PartialViewResult EditProductPVAJAX(int pid, string altName, string altDesc, int altStock) { List <SImpleWebApp.Models.T_products> plist = new List <SImpleWebApp.Models.T_products>(); T_products tp = DB.T_products.Find(pid); if (altName != "") { tp.P_name = altName; } if (altDesc != "") { tp.P_description = altDesc; } if (altStock != -1) { tp.P_stock = altStock; } /*if(altStock < 0) * { * * tp.P_stock = 0; * } * else * { * tp.P_stock = altStock; * }*/ DB.T_products.AddOrUpdate(tp); int res = DB.SaveChanges(); if (res > 0) { Response.Write("Selected Product was edited successfuly"); } else { Response.Write("Operation Failed"); } var query = from st in DB.T_products where st.P_name.Contains("") select st; plist = query.ToList(); return(PartialView("SearchProductPVAJAX", plist)); }
public PartialViewResult RemoveProductPVAJAX(int [] pids) { demo_appEntities1 DB = new demo_appEntities1(); List <SImpleWebApp.Models.T_products> plist = new List <SImpleWebApp.Models.T_products>(); List <SImpleWebApp.Models.T_products> plistToRem = new List <SImpleWebApp.Models.T_products>(); //remove duplicates that arrive from the view jquery method //TODO: (need to check why duplicates arrive to controller) int[] pidDistinct = pids.Distinct().ToArray(); for (int i = 0; i < pidDistinct.Length; i++) { T_products tp = DB.T_products.Find(pidDistinct[i]); plistToRem.Add(tp); } DB.T_products.RemoveRange(plistToRem); int res; try { res = DB.SaveChanges(); } catch (Exception e) { throw e; } if (res > 0) { Response.Write("new product was removed successfuly"); } else { Response.Write("Operation Failed"); } var query = from st in DB.T_products where st.P_name.Contains("") select st; plist = query.ToList(); return(PartialView("SearchProductPVAJAX", plist)); }