public ActionResult DeleteAll(string[] id) //id is an array for choosing multiple product to delete. { int[] getid = null; //initialize getid as null. if (id != null) { getid = new int[id.Length]; //if getid is not null int j = 0; //initialize j foreach (string i in id) //perform following for every i in id. { int.TryParse(i, out getid[j++]); } using (DBProduct2 db = new DBProduct2()) { List <ProductManage> pids = new List <ProductManage>(); //pids is variable of Table ProductManage. pids = db.ProductManages.Where(x => getid.Contains(x.ID)).ToList(); foreach (var s in pids) //it will delete all the selected products form the list. { db.ProductManages.Remove(s); } db.SaveChanges(); //following code is for toaster message for 5 second. HttpCookie cookie3 = new HttpCookie("CookieDeleteAll"); cookie3.Value = "DeleteAll"; Response.Cookies.Add(cookie3); cookie3.Expires = DateTime.Now.AddSeconds(5); return(RedirectToAction("Index")); } } return(RedirectToAction("Index")); }
DBProduct2 db = new DBProduct2(); //db is the object of Entity DBProduct2 //GET: Index public ActionResult Index(string SearchBy, string Search, int?page, string sortBy, string AdvanceSearch) //Passing Argument for Search,Advance Search and Sort. { using (DBProduct2 db = new DBProduct2()) { ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Product_Name desc" : ""; //sort by Product Name. ViewBag.SortCategoryParameter = string.IsNullOrEmpty(sortBy) /*sortBy == "Category" */? "Category desc" : "Category"; //sort by Category. var plist = db.ProductManages.AsQueryable(); //plist is variable of Queryable. List <ProductManage> productlist = db.ProductManages.ToList(); //productlist is variable of List. if (SearchBy == "Category") //Search by Category. { plist = plist.Where(x => x.Category.StartsWith(Search) || Search == null); } else if (SearchBy == "Product_Name") //Search by Product Name. { plist = plist.Where(x => x.Product_Name.StartsWith(Search) || Search == null); } else if (AdvanceSearch != null) //Advance Search with all attributes. { return(View(db.ProductManages.Where(x => x.Product_Name.StartsWith(AdvanceSearch) || x.Category.StartsWith(AdvanceSearch) || x.Short_Description.StartsWith(AdvanceSearch) || x.Long_Description.StartsWith(AdvanceSearch) || Search == null).ToList().ToPagedList(page ?? 1, 3))); } switch (sortBy) //Switch Case For sorting. { case "Product_Name desc": //Prooduct Name Sorted By Descending Order. plist = plist.OrderByDescending(x => x.Product_Name); break; case "Category desc": //Category Sorted By Descending Order. plist = plist.OrderByDescending(x => x.Category); break; case "Category": //Category Sorted By Ascending Order. plist = plist.OrderBy(x => x.Category); break; default: //Prooduct Name Sorted By Ascending Order. plist = plist.OrderBy(x => x.Product_Name); break; } return(View(plist.ToPagedList(page ?? 1, 5))); //Display 5 Items(Products) in Index View. } }