排球积分程序(五)——Controller的编码
VolleyballController
using MvcVolleyball.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcVolleyball.Controllers { public class VolleyballController : Controller { // // GET: /Volleyball/ private VolleyballDBContext db = new VolleyballDBContext(); public ActionResult Index()//选择功能 { return View(); } public ActionResult Count()//选择队伍 { ViewBag.teamId = new SelectList(db.teams, "teamId", "teamName"); return View(); } [HttpPost] public ActionResult Count(string team)//将选择队伍的信息接收病发送 { team = Request.Form[0]; if (team != null) { return RedirectToAction("CountScore", new { teamId=team}); } return View(); } public ActionResult CountScore(string teamId)//进行计分 { ViewBag.teamId = teamId; string [] teamsId=teamId.Split(','); int hostTeamId = Convert.ToInt32( teamsId[0]); int guestTeamId =Convert.ToInt32( teamsId[1]); var hostTeam = from d in db.teams where d.teamId == hostTeamId select d.teamName; var guestTeam = from d in db.teams where d.teamId == guestTeamId select d.teamName; foreach (var name in hostTeam) { ViewBag.hostteam = name; } foreach (var name in guestTeam) { ViewBag.guestteam = name; } var hostMemberlst = new List<string>(); var guestMemberlst = new List<string>(); var guestMember = from d in db.teamMembers where d.teamId == guestTeamId select d.name; var hostMember = from d in db.teamMembers where d.teamId == hostTeamId select d.name; hostMemberlst.AddRange(hostMember); guestMemberlst.AddRange(guestMember); ViewBag.hostMember = new SelectList(hostMemberlst); ViewBag.guestMember = new SelectList(guestMemberlst); var hostteamscores = from d in db.hostTeamScores where d.teamId == hostTeamId select d.hostTeamScore; foreach (var item in hostteamscores) { ViewBag.hostteamscores = item; } var guestteamscores = from d in db.guestTeamScores where d.teamId == guestTeamId select d.guestTeamScore; foreach (var item in guestteamscores) { ViewBag.guestteamscores = item; } return View(); } public ActionResult addScore(string teamIds,HostTeamScore host,GuestTeamScore guest)//加分 { host.hostTeamScore++; db.hostTeamScores.Add(host); db.SaveChanges(); guest.guestTeamScore++; db.guestTeamScores.Add(guest); db.SaveChanges(); return RedirectToAction("CountScore", new { teamId = teamIds }); } public ActionResult Search()//查询,选择队伍 { ViewBag.teamId = new SelectList(db.teams, "teamId", "teamName"); return View(); } [HttpPost] public ActionResult Search(string teamId)//将队伍Id传过去 { teamId = Request.Form[0]; return RedirectToAction("TeamScore",new{teamIds=int.Parse(teamId)}); } public ActionResult TeamScore(int teamIds)//显示队伍得分信息 { var teamScore = from d in db.hostTeamScores where d.teamId == teamIds select d; return View(teamScore.ToList()); } } }