Files
StockMan/StockManMVC/Controllers/LocationsController.cs
T
2016-10-06 09:22:05 +02:00

168 lines
5.1 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;
namespace StockManMVC.Controllers
{
public class LocationsController : Controller
{
private StockManEntities db = new StockManEntities();
// GET: Locations/LocTypeID
public ActionResult Index(string LocTypeID)
{
if (LocTypeID == null) LocTypeID = "";
ViewBag.LocTypeID = new SelectList(db.LocType, "ID", "Descr", LocTypeID);
ViewBag.LocTypeSel = LocTypeID;
var location = db.Location.Include(l => l.LocType);
return View(location.ToList());
}
// GET: Location/ListByType/LocTypeID
public ActionResult ListByType(string LocTypeID)
{
var CurrLocations = db.Location.Where(s => s.ID != "");
if (LocTypeID.ToString() != "")
{
CurrLocations = db.Location.Where(s => s.LocTypeID == LocTypeID);
}
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);
}
}
}