Skip to content

Instantly share code, notes, and snippets.

@danieleli
Last active December 20, 2017 14:05
Show Gist options
  • Select an option

  • Save danieleli/1672067 to your computer and use it in GitHub Desktop.

Select an option

Save danieleli/1672067 to your computer and use it in GitHub Desktop.
Generic Controller Blogpost
public abstract class ModelRoot
{
public virtual int Id { get; set; }
}
public class Foo : ModelRoot
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
// Generic Controller base class
public abstract class GController<TModel> : Controller where TModel : ModelRoot
{
private readonly Mvc4Context _context = new Mvc4Context();
// Public accessors for dbContext
public Mvc4Context DbContext { get { return _context; } }
public virtual ViewResult Index()
{
return View(_context.Set<TModel>().ToList());
}
public virtual ViewResult Details(int id)
{
return View(_context.Set<TModel>().Find(id));
}
public virtual ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(TModel model)
{
if (ModelState.IsValid)
{
_context.Set<TModel>().Add(model);
_context.SaveChanges();
if (Request.IsAjaxRequest())
{
return Json(model, JsonRequestBehavior.AllowGet);
}
return RedirectToAction("Index");
}
return View();
}
public virtual ActionResult Edit(int id)
{
return View(_context.Set<TModel>().Find(id));
}
[HttpPost]
public virtual ActionResult Edit(TModel model)
{
if (ModelState.IsValid)
{
_context.Entry(model).State = EntityState.Modified;
_context.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
public ActionResult Delete(int id)
{
return View(_context.Set<TModel>().Find(id));
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
var entity = _context.Set<TModel>().Find(id);
_context.Set<TModel>().Remove(entity);
_context.SaveChanges();
return RedirectToAction("Index");
}
}
// Foo controller implemented with generic controller
public class FooController : GController<Foo>
{
public override ViewResult Index()
{
return View(DbContext.Set<Foo>().Include(foo => foo.Bar));
}
}
// standard foo controller implementation
public class FoosController : Controller
{
private Mvc4Context context = new Mvc4Context();
public ViewResult Index()
{
return View(context.Foos.ToList());
}
public ViewResult Details(int id)
{
Foo foo = context.Foos.Single(x => x.Id == id);
return View(foo);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Foo foo)
{
if (ModelState.IsValid)
{
context.Foos.Add(foo);
context.SaveChanges();
return RedirectToAction("Index");
}
return View(foo);
}
public ActionResult Edit(int id)
{
Foo foo = context.Foos.Single(x => x.Id == id);
return View(foo);
}
[HttpPost]
public ActionResult Edit(Foo foo)
{
if (ModelState.IsValid)
{
context.Entry(foo).State = EntityState.Modified;
context.SaveChanges();
return RedirectToAction("Index");
}
return View(foo);
}
public ActionResult Delete(int id)
{
Foo foo = context.Foos.Single(x => x.Id == id);
return View(foo);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Foo foo = context.Foos.Single(x => x.Id == id);
context.Foos.Remove(foo);
context.SaveChanges();
return RedirectToAction("Index");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment