排球计分程序(五)—— Controller的设计与实现

Controllers: 处理浏览器的请求,取得数据模型,然后指定要响应浏览器请求的视图模板。

控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,

然后用确定用哪个视图来显示模型处理返回的数据。

比赛计分控制器:

运动员信息控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 排球计分程序.Models;

namespace 排球计分程序.Controllers
{
    public class BallsController : Controller
    {
        private BallDBContext db = new BallDBContext();

        //
        // GET: /Balls/

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

        //
        // GET: /Balls/Details/5

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

        //
        // GET: /Balls/Create

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

        //
        // POST: /Balls/Create

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

            return View(ball);
        }

        //
        // GET: /Balls/Edit/5

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

        //
        // POST: /Balls/Edit/5

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

        //
        // GET: /Balls/Delete/5

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

        //
        // POST: /Balls/Delete/5

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

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

队伍公告控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 排球队伍公告信息.Models;
using 排球队伍公告信息.Models.Entities;
using 排球队伍公告信息.Models.Interfaces;

namespace 排球队伍公告信息.Controllers
{
    public class AnnounceController : Controller
    {
        //
        // GET: /Announce/

       public ActionResult Release()
       {
           ICategoryService cServ = ServiceBuilder.BuildCategoryService();
           List<CategoryInfo> categories = cServ.GetAll();
           ViewData["Categories"] = new SelectList(categories, "ID", "Name");
           return View("Release");
       }
        public ActionResult DoRelease()
       {
           if (String.IsNullOrEmpty(Request.Form["Title"]) || String.IsNullOrEmpty(Request.Form["Content"]))
           {
               if (String.IsNullOrEmpty(Request.Form["Title"]))
               {
                   ViewData.ModelState.AddModelError("TitleValidator","公告标题不能为空!");
               }
               if (String.IsNullOrEmpty(Request.Form["Content"]))
               {
                   ViewData.ModelState.AddModelError("ContentValidator", "公告内容不能为空!");
               }
                return Release();
           }
            AnnounceInfo announce = new AnnounceInfo()
           {
               ID = 1,
               Title = Request.Form["Title"],
               Category = Int32.Parse(Request.Form["Category"]),
               Content = Request.Form["Content"],
           };
           IAnnounceService aServ = ServiceBuilder.BuildAnnounceService();
           aServ.Release(announce);
            ViewData["Announce"] = announce;
           return View("ReleaseSucceed");
       }
    }
}

posted @ 2017-06-22 11:00  孙栋梁  阅读(199)  评论(0编辑  收藏  举报