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 X.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 // GET: Locations/LocTypeID public ActionResult Index(string LocTypeID, string SearchVal, string SortOrd, int? page, int? pageSize) { // fix campi nulli if (LocTypeID == null) LocTypeID = ""; if (SearchVal == null) SearchVal = ""; if (SortOrd == null) SortOrd = ""; if (page == null) page = 1; if (pageSize == null) pageSize = 10; // salvo in model valori ViewBag.LocTypeDD = new SelectList(db.LocType, "ID", "Descr", LocTypeID); ViewBag.LocTypeID = LocTypeID; ViewBag.SearchVal = SearchVal; ViewBag.SortOrd = SortOrd; ViewBag.page = page; ViewBag.pageSize = pageSize; return View(); } // GET: Location/ListByType/LocTypeID public ActionResult ListByType(string LocTypeID, string SearchVal, string SortOrd, int? page, int? pageSize) { // gestione selettore dim pagina if (page == null) page = 1; if (pageSize == null) pageSize = 10; ViewBag.pageSize = pageSize; var pageOptions = new[] { "5", "10", "25", "50", "100" }; ViewBag.PageSizeDD = new SelectList(pageOptions, pageSize.ToString()); if (LocTypeID == null) LocTypeID = ""; if (SearchVal == null) SearchVal = ""; if (SortOrd == null) SortOrd = ""; ViewBag.LocTypeID = LocTypeID; ViewBag.SearchVal = SearchVal; ViewBag.SortOrd = SortOrd; 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; } // ritorno modello paginato! var pagedData = CurrLocations.ToPagedList((int)page, (int)pageSize); return PartialView("_ListByType", pagedData); } // 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 = "Descr,LocTypeID")] Location location) { // ID deve essere calcolato a partire dal LocTypeID + contatore 6 cifre... chiamo la stored x recuperare nuovo contatore int nextNum = 0; using (var ctx = new StockManEntities()) { // esegue stored procedure come function, recuperando nuovo ID creato... var newCounter = ctx.stp_Counters_getNext(location.LocTypeID).ToList(); nextNum = newCounter[0].NextNum; } // costruisco nuovo location ID univoco location.ID = string.Format("{0}{1:000000}", location.LocTypeID, nextNum); // ora devo eliminare gli errori pendenti x il campo ID ModelState["ID"].Errors.Clear(); // va ri-validato il modello UpdateModel(location); // controllo se modello dati sia ok.. 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) { // recupero vecchi valori... Location locationOld = db.Location.Find(location.ID); // imposto valori vecchi x campi "bloccati" (tipo locazione...) location.LocTypeID = locationOld.LocTypeID; // ora devo eliminare gli errori pendenti x il campo ID ModelState["LocTypeID"].Errors.Clear(); // va ri-validato il modello UpdateModel(location); // verifico! if (ModelState.IsValid) { // sblocco vecchio item x non fare conflitto db.Entry(locationOld).State = EntityState.Detached; // indico che devo salvare nuovo! 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); } } }