using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using StockManMVC.Models; using PagedList; namespace StockManMVC.Controllers { public class LocationsController : Controller { private StockManEntities db = new StockManEntities(); // vedere questi link x completare: // https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application // http://devproconnections.com/aspnet-mvc/aspnet-mvc-paging-done-perfectly // GET: Locations/LocTypeID public ActionResult Index(string LocTypeID, string SearchVal, string SortOrd) { if (LocTypeID == null) LocTypeID = ""; if (SearchVal == null) SearchVal = ""; if (SortOrd == null) SortOrd = ""; ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr", LocTypeID); ViewBag.LocTypeSel = LocTypeID; ViewBag.SearchVal = SearchVal; var location = db.Location.Include(l => l.LocType); return View(location.ToList()); } // GET: Location/ListByType/LocTypeID public ActionResult ListByType(string LocTypeID, string SearchVal, string SortOrd) { if (LocTypeID == null) LocTypeID = ""; if (SearchVal == null) SearchVal = ""; if (SortOrd == null) SortOrd = ""; ViewBag.LocTypeSel = LocTypeID; ViewBag.SearchVal = SearchVal; ViewBag.DescrSortParm = String.IsNullOrEmpty(SortOrd) ? "Descr_desc" : ""; ViewBag.LocTypeSortParm = SortOrd == "LocType" ? "LocType_desc" : "LocType"; var CurrLocations = db.Location.Where(s => s.ID != ""); if (LocTypeID.ToString() != "") { CurrLocations = db.Location.Where(s => s.LocTypeID == LocTypeID); } if(SearchVal != "") { CurrLocations = CurrLocations.Where(s => s.Descr.Contains(SearchVal)); // || s.LocType.Descr.Contains(SearchVal) } // ora verifico ordinamento... switch (SortOrd) { case "Descr_desc": CurrLocations = CurrLocations.OrderByDescending(s => s.Descr); break; case "LocType": CurrLocations = CurrLocations.OrderBy(s => s.LocType.Descr); break; case "LocType_desc": CurrLocations = CurrLocations.OrderByDescending(s => s.LocType.Descr); break; default: CurrLocations = CurrLocations.OrderBy(s => s.Descr); break; } ViewBag.LocTypeID = LocTypeID; return PartialView("_ListByType", CurrLocations.ToList()); } // GET: Locations/Create (mode=std|full, opzionale) public ActionResult Create(string mode) { ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr"); if (mode == null) { mode = "full"; } if (mode == "std") { return PartialView(); } else { return View(); } } // POST: Locations/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ID,Descr,LocTypeID")] Location location) { if (ModelState.IsValid) { db.Location.Add(location); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr", location.LocTypeID); return View(location); } // GET: Locations/Edit/5 (mode=std|full, opzionale) public ActionResult Edit(string id, string mode) { if (mode == null) { mode = "full"; } if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Location location = db.Location.Find(id); if (location == null) { return HttpNotFound(); } ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr", location.LocTypeID); if (mode == "std") { return PartialView(location); } else { return View(location); } } // POST: Locations/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "ID,Descr,LocTypeID")] Location location) { if (ModelState.IsValid) { db.Entry(location).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr", location.LocTypeID); return View(location); } // GET: Locations/Delete/5 (mode=std|full, opzionale) public ActionResult Delete(string id, string mode) { if (mode == null) { mode = "full"; } if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Location location = db.Location.Find(id); if (location == null) { return HttpNotFound(); } if (mode == "std") { return PartialView(location); } else { return View(location); } } // POST: Locations/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(string id) { Location location = db.Location.Find(id); db.Location.Remove(location); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }