Files
StockMan/StockManMVC/Controllers/LocationsController.cs
T
2016-10-21 09:30:30 +02:00

211 lines
7.2 KiB
C#

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
// GET: Locations/LocTypeID
public ActionResult Index(string LocTypeID, string SearchVal, string SortOrd, int? page, int? pageSize)
{
if (LocTypeID == null) LocTypeID = "";
if (SearchVal == null) SearchVal = "";
if (SortOrd == null) SortOrd = "";
if (page == null) page = 1;
if (pageSize == null) pageSize = 5;
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)
{
if (LocTypeID == null) LocTypeID = "";
if (SearchVal == null) SearchVal = "";
if (SortOrd == null) SortOrd = "";
//if (page == null) page = 1;
//if (pageSize == null) pageSize = 5;
int PageSize = 5;
int PageNum = (page ?? 1);
ViewBag.LocTypeID = 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());
//return PartialView("_ListByType", CurrLocations.ToPagedList(PageNum, PageSize));
}
// 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);
}
}
}