Created
June 8, 2012 17:36
-
-
Save jholt456/2897131 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(Html.Telerik() | |
.Grid(Model) | |
.Name("Todos")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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