MVC4中control的增删改查

 public class TestController : Controller
    {
        private LeaveEntities db = new LeaveEntities();

        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View(db.termtimes.ToList());
        }

        //
        // GET: /Test/Details/5

        public ActionResult Details(int id = 0)
        {
            termtime termtime = db.termtimes.Find(id);
            if (termtime == null)
            {
                return HttpNotFound();
            }
            return View(termtime);
        }

        //
        // GET: /Test/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Test/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(termtime termtime)
        {
            if (ModelState.IsValid)
            {
                db.termtimes.Add(termtime);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(termtime);
        }

        //
        // GET: /Test/Edit/5

        public ActionResult Edit(int id = 0)
        {
            termtime termtime = db.termtimes.Find(id);
            if (termtime == null)
            {
                return HttpNotFound();
            }
            return View(termtime);
        }

        //
        // POST: /Test/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(termtime termtime)
        {
            if (ModelState.IsValid)
            {
                db.Entry(termtime).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(termtime);
        }

        //
        // GET: /Test/Delete/5

        public ActionResult Delete(int id = 0)
        {
            termtime termtime = db.termtimes.Find(id);
            if (termtime == null)
            {
                return HttpNotFound();
            }
            return View(termtime);
        }

        //
        // POST: /Test/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            termtime termtime = db.termtimes.Find(id);
            db.termtimes.Remove(termtime);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
原文地址:https://www.cnblogs.com/lunawzh/p/6524723.html