Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created December 21, 2016 10:14
Show Gist options
  • Save abel-masila/1842ab162c12e0a4140446b03ebb8606 to your computer and use it in GitHub Desktop.
Save abel-masila/1842ab162c12e0a4140446b03ebb8606 to your computer and use it in GitHub Desktop.
The Controller
using ProductsApp.Context;
using ProductsApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace ProductsApp.Controllers
{
public class ProductController : Controller
{
private ProductContext db = new ProductContext();
// GET: Product
public ActionResult Index()
{
return View(db.Products.ToList());
}
// GET: Product/Details/5
public ActionResult Details(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
return View(product);
}
// GET: Product/Create
[HttpGet]
public ActionResult Create()
{
return View();
}
// POST: Product/Create
[HttpPost]
public ActionResult Create(Product product)
{
try
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
// GET: Product/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
return View(product);
}
// POST: Product/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
try
{
// TODO: Add update logic here
if (ModelState.IsValid)
{
db.Entry(product).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
// GET: Product/Delete/5
[HttpGet]
public ActionResult Delete(int? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Product product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
return View(product);
}
// POST: Product/Delete/5
[HttpPost]
public ActionResult Delete(int? id, Product product)
{
try
{
// TODO: Add delete logic here
if (ModelState.IsValid)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
product = db.Products.Find(id);
if (product == null)
return HttpNotFound();
db.Products.Remove(product);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(product);
}
catch
{
return View();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment