首页 > 代码库 > MVC系列博客之排球计分(六)Controller的实现(二)

MVC系列博客之排球计分(六)Controller的实现(二)

上一篇博客没有完整的展现controller的代码,现在展示一下

HomeController

技术分享

CoachController代码如下

 public class CoachController : Controller
    {
        private gDBContext db = new gDBContext();

        //
     

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

        //
      

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

        //
      

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

        //
      

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

            return View(Score);
        }

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



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

        //


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

        //
       

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Score gzscore = db.Students.Find(id);
            db.Students.Remove(score);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

至此,controller已全部完成。

MVC系列博客之排球计分(六)Controller的实现(二)