using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; namespace Projects.Models { public class ProjectsController : Controller { private ProjectsDBEntities db = new ProjectsDBEntities(); // GET: Projects public ActionResult Index() { var projects = db.Projects.Include(p => p.User); return View(projects.ToList()); } // GET: Projects/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Project project = db.Projects.Find(id); if (project == null) { return HttpNotFound(); } return View(project); } // GET: Projects/Create public ActionResult Create() { ViewBag.user_id = new SelectList(db.Users, "id", "login"); return View(); } // POST: Projects/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "id,user_id,name,description")] Project project) { if (ModelState.IsValid) { project.created_at = DateTime.Now; project.updated_at = DateTime.Now; db.Projects.Add(project); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id); return View(project); } // GET: Projects/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Project project = db.Projects.Find(id); if (project == null) { return HttpNotFound(); } ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id); return View(project); } // POST: Projects/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "id,user_id,name,description,created_at")] Project project) { if (ModelState.IsValid) { project.updated_at = DateTime.Now; db.Entry(project).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.user_id = new SelectList(db.Users, "id", "login", project.user_id); return View(project); } // GET: Projects/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Project project = db.Projects.Find(id); if (project == null) { return HttpNotFound(); } return View(project); } // POST: Projects/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Project project = db.Projects.Find(id); db.Projects.Remove(project); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }