排球计分程序(五)—— 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");
       }
    }
}

转载于:https://www.cnblogs.com/sdl1305702018/p/7063825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值