2 using System.Collections.Generic;
4 using System.Data.Entity;
10 namespace Projects.Models
12 public class ProjectsController : Controller
14 private ProjectsDBEntities db = new ProjectsDBEntities();
17 public ActionResult Index()
19 var projects = db.Projects.Include(p => p.User);
20 return View(projects.ToList());
23 // GET: Projects/Details/5
24 public ActionResult Details(int? id)
28 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
30 Project project = db.Projects.Find(id);
33 return HttpNotFound();
38 // GET: Projects/Create
39 public ActionResult Create()
41 ViewBag.user_id = new SelectList(db.Users, "id", "login");
45 // POST: Projects/Create
46 // To protect from overposting attacks, please enable the specific properties you want to bind to, for
47 // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
49 [ValidateAntiForgeryToken]
50 public ActionResult Create([Bind(Include = "id,user_id,name,description")] Project project)
52 if (ModelState.IsValid)
54 project.created_at = DateTime.Now;
55 project.updated_at = DateTime.Now;
56 db.Projects.Add(project);
58 return RedirectToAction("Index");
61 ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
65 // GET: Projects/Edit/5
66 public ActionResult Edit(int? id)
70 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
72 Project project = db.Projects.Find(id);
75 return HttpNotFound();
77 ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
81 // POST: Projects/Edit/5
82 // To protect from overposting attacks, please enable the specific properties you want to bind to, for
83 // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
85 [ValidateAntiForgeryToken]
86 public ActionResult Edit([Bind(Include = "id,user_id,name,description,created_at")] Project project)
88 if (ModelState.IsValid)
90 project.updated_at = DateTime.Now;
91 db.Entry(project).State = EntityState.Modified;
93 return RedirectToAction("Index");
95 ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id);
99 // GET: Projects/Delete/5
100 public ActionResult Delete(int? id)
104 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
106 Project project = db.Projects.Find(id);
109 return HttpNotFound();
111 return View(project);
114 // POST: Projects/Delete/5
115 [HttpPost, ActionName("Delete")]
116 [ValidateAntiForgeryToken]
117 public ActionResult DeleteConfirmed(int id)
119 Project project = db.Projects.Find(id);
120 db.Projects.Remove(project);
122 return RedirectToAction("Index");
125 protected override void Dispose(bool disposing)
131 base.Dispose(disposing);