Skip to content

Instantly share code, notes, and snippets.

@jholt456
Created June 8, 2012 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jholt456/2897131 to your computer and use it in GitHub Desktop.
Save jholt456/2897131 to your computer and use it in GitHub Desktop.
public class CategoryController : Controller
{
private TodoDataContext db = new TodoDataContext();
//
// GET: /Category/
public ViewResult Index()
{
return View(db.Categories.ToList());
}
//
// GET: /Category/Details/5
public ViewResult Details(int id)
{
Category category = db.Categories.Find(id);
return View(category);
}
//
// GET: /Category/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Category/Create
[HttpPost]
public ActionResult Create(Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
//
// GET: /Category/Edit/5
public ActionResult Edit(int id)
{
Category category = db.Categories.Find(id);
return View(category);
}
//
// POST: /Category/Edit/5
[HttpPost]
public ActionResult Edit(Category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
//
// GET: /Category/Delete/5
public ActionResult Delete(int id)
{
Category category = db.Categories.Find(id);
return View(category);
}
//
// POST: /Category/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Categories.Find(id);
db.Categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
public class TodoDataContext : DbContext
{
public DbSet<Todo> Todo { get; set; }
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Todo>()
.HasOptional(x => x.Category)
.WithMany(i => i.Todos);
base.OnModelCreating(modelBuilder);
}
}
public class Todo
{
public Todo()
{
this.DateCreated = DateTime.Now;
}
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; private set; }
public Category Category { get; set; }
public int? CategoryId { get; set; }
}
public class Category
{
public Category()
{
this.DateCreated = DateTime.Now;
this.Todos = new List<Todo>();
}
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; private set; }
public virtual ICollection<Todo> Todos { get; set; }
}
@(Html.Telerik()
.Grid(Model)
.Name("Todos"))
public class TodoController : Controller
{
private TodoDataContext db = new TodoDataContext();
//
// GET: /Todo/
public ViewResult Index()
{
var todoes = db.Todoes.Include(t => t.Category);
return View(todoes.ToList());
}
//
// GET: /Todo/Details/5
public ViewResult Details(int id)
{
Todo todo = db.Todoes.Find(id);
return View(todo);
}
//
// GET: /Todo/Create
public ActionResult Create()
{
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
return View();
}
//
// POST: /Todo/Create
[HttpPost]
public ActionResult Create(Todo todo)
{
if (ModelState.IsValid)
{
db.Todoes.Add(todo);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", todo.CategoryId);
return View(todo);
}
//
// GET: /Todo/Edit/5
public ActionResult Edit(int id)
{
Todo todo = db.Todoes.Find(id);
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", todo.CategoryId);
return View(todo);
}
//
// POST: /Todo/Edit/5
[HttpPost]
public ActionResult Edit(Todo todo)
{
if (ModelState.IsValid)
{
db.Entry(todo).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", todo.CategoryId);
return View(todo);
}
//
// GET: /Todo/Delete/5
public ActionResult Delete(int id)
{
Todo todo = db.Todoes.Find(id);
return View(todo);
}
//
// POST: /Todo/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Todo todo = db.Todoes.Find(id);
db.Todoes.Remove(todo);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment