排球计分程序重构(五中篇)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排球计分程序重构(五中篇)相关的知识,希望对你有一定的参考价值。
代码讲解2
TeamsController
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MVCVolleyball.Controllers 8 { 9 public class TeamsController : BaseController 10 { 11 12 #region 获取队伍列表 13 /// <summary> 14 /// 获取队伍列表 15 /// </summary> 16 /// <returns></returns> 17 [HttpGet] 18 public ActionResult GetTeams() 19 { 20 return View(); 21 } 22 [HttpPost] 23 public ActionResult GetTeams(long page, long limit) 24 { 25 PetaPoco.Page<Team> list = Team.Page(page, limit, "select * from Team"); 26 Data data = new Data(); 27 data.count = list.TotalItems; 28 data.data = new List<dynamic>(); 29 foreach (var item in list.Items) 30 { 31 data.data.Add(new { Id = item.Id, TeamName = item.TeamName }); 32 } 33 return Json(data); 34 } 35 #endregion 36 37 #region 添加队伍 38 /// <summary> 39 /// 添加队伍 40 /// </summary> 41 /// <returns></returns> 42 [HttpGet] 43 public ActionResult AddTeam() 44 { 45 return View(); 46 } 47 [HttpPost] 48 public ActionResult AddTeam(string teamName) 49 { 50 string msg = ""; 51 if (teamName == "" || teamName == null) 52 { 53 msg = "请输入队名"; 54 } 55 else 56 { 57 Team team = new Team(); 58 team.TeamName = teamName; 59 db.Insert(team); 60 msg = "添加成功"; 61 } 62 return Json(msg); 63 } 64 #endregion 65 66 } 67 }
TeamsController的视图
有两个,一个是获取队伍列表,另一个是添加队伍
队伍列表
1 @{ 2 ViewBag.Title = "队伍信息"; 3 } 4 5 <table id="team" style="margin:0 30%;position:fixed"></table> 6 7 <script> 8 layui.use(‘table‘, function () { 9 var table = layui.table; 10 table.render({ 11 elem: ‘#team‘ 12 , url: ‘/Teams/GetTeams/‘ 13 , method: ‘post‘ 14 , limits: [10, 20, 30, 40, 50] 15 , limit: 20 //默认采用60 16 , page: true 17 , cols: [[ //标题栏 18 { space: true } 19 , { field: ‘TeamName‘, title: ‘队名‘, align: ‘center‘, width: 1165} 20 ,{ space: true } 21 ]] 22 }); 23 }); 24 </script>
添加队伍
1 @{ 2 ViewBag.Title = "添加队伍"; 3 } 4 @using (Ajax.BeginForm(new AjaxOptions { 5 Url= "/Teams/AddTeam", 6 HttpMethod = "Post", 7 OnSuccess = "success", 8 OnBegin = "" 9 })) 10 { 11 <div class="layui-form" style="width:100%;height:300px;"> 12 <div style="margin-left:35%; margin-bottom:30px;margin-top:10px;" ><img class="layui-anim layui-anim-fadein layui-anim-rotate " src="~/Content/Image/rabbit.gif"/></div> 13 <div class="layui-form-item"> 14 <label class="layui-form-label">队名</label> 15 <div class="layui-input-block"> 16 <input type="text" name="teamName" id="teamName" placeholder="请输入队名" autocomplete="off" class="layui-input"> 17 </div> 18 </div> 19 <div class="layui-form-item"> 20 <div class="layui-input-block" style="text-align:center;"> 21 <input type="submit" class="layui-btn" id="btn" value="立即提交" /> 22 </div> 23 </div> 24 </div> 25 } 26 <script> 27 function success(data) { 28 if (data == "添加成功") { 29 layer.msg(data, { icon: 1 }); 30 setTimeout("window.location = ‘/Teams/GetTeams‘", 2000); 31 } 32 else { 33 layer.msg(data, { icon: 2 }); 34 } 35 } 36 </script>
TeamMemberController
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Threading; 7 8 namespace MVCVolleyball.Controllers 9 { 10 public class TeamMemberController : BaseController 11 { 12 13 #region 查看队员先列出所有队伍 14 /// <summary> 15 /// 查看队员先列出所有队伍 16 /// </summary> 17 /// <returns></returns> 18 public ActionResult ChooseTeam() 19 { 20 List<Team> teamList = Team.Fetch(""); 21 return View(teamList); 22 } 23 #endregion 24 25 #region 列出某一队伍的全部队员 26 /// <summary> 27 /// 列出某一队伍的全部队员 28 /// </summary> 29 /// <param name="teamId"></param> 30 /// <param name="page"></param> 31 /// <param name="limit"></param> 32 /// <returns></returns> 33 [HttpPost] 34 public ActionResult GetTeamMember(int teamId, long page, long limit) 35 { 36 PetaPoco.Page<TeamMember> list = TeamMember.Page(page, limit, "where [email protected] ", teamId); 37 Data data = new Data(); 38 data.count = list.TotalItems; 39 data.data = new List<dynamic>(); 40 foreach (var item in list.Items) 41 { 42 string teamName = Team.FirstOrDefault("where Id=" + item.TeamId + "").TeamName; 43 data.data.Add(new 44 { 45 Id = item.Id, 46 JerseyNumber = item.JerseyNumber, 47 MembersName = item.MembersName, 48 Position = item.Position, 49 Stature = item.Stature, 50 Age = item.Age, 51 TeamName = teamName 52 }); 53 } 54 return Json(data); 55 } 56 #endregion 57 58 #region 添加队员 59 /// <summary> 60 /// 添加队员 61 /// </summary> 62 /// <returns></returns> 63 [HttpGet] 64 public ActionResult AddTeamMember() 65 { 66 List<Team> teamList = Team.Fetch(""); 67 ViewBag.TeamList = teamList; 68 List<Position> positionList = Position.Fetch(""); 69 ViewBag.PositionList = positionList; 70 return View(); 71 } 72 [HttpPost] 73 public ActionResult AddTeamMember(TeamMember model) 74 { 75 76 db.Insert(model); 77 Thread.Sleep(2000); 78 return Redirect("ChooseTeam"); 79 } 80 #endregion 81 } 82 }
TeamMemberController的视图
依旧是两个,只不过与队伍的视图不同的是,队员有一个所属队伍,所以要把队伍先列出来,然后动态加载队员的列表
然后就是添加队员的视图
队员列表视图
1 @model List<Team> 2 @{ 3 ViewBag.Title = "请选择队伍"; 4 } 5 <div style="height:300px;margin:30px auto;"> 6 @{ 7 foreach (var item in Model) 8 { 9 <button class="layui-btn layui-btn-normal" style="margin-top:10px;" onclick="ShowMember(@item.Id)">@item.TeamName</button> 10 } 11 } 12 <table id="member" style="margin:0 auto;"></table> 13 </div> 14 <script> 15 function ShowMember(teamId) 16 { 17 layui.use(‘table‘, function () { 18 var table = layui.table; 19 table.render({ 20 elem: ‘#member‘ 21 , url: ‘/TeamMember/GetTeamMember/‘ 22 , where: { teamId: teamId } 23 , method: ‘post‘ 24 , limits: [10, 20, 30, 40, 50] 25 , limit: 20 //默认采用60 26 , page: true 27 , cols: [[ //标题栏 28 { field: ‘TeamName‘, title: ‘所属队伍‘, align: ‘center‘, width: 196 } 29 , { field: ‘JerseyNumber‘, title: ‘球衣号码‘, align: ‘center‘, width: 196 } 30 , { field: ‘MembersName‘, title: ‘姓名‘, align: ‘center‘, width: 196 } 31 , { field: ‘Position‘, title: ‘位置‘, align: ‘center‘, width: 196 } 32 , { field: ‘Stature‘, title: ‘身高(CM)‘, align: ‘center‘, width: 196 } 33 , { field: ‘Age‘, title: ‘年龄(岁)‘, align: ‘center‘, width: 196 } 34 ]] 35 }); 36 }); 37 } 38 </script>
添加队员
1 @{ 2 ViewBag.Title = "添加队员"; 3 } 4 <form class="layui-form" action="/TeamMember/AddTeamMember" method="post" style="margin-top:30px;"> 5 <div class="layui-form-item"> 6 <label class="layui-form-label">请选择队伍</label> 7 <div class="layui-input-block"> 8 <select name="TeamId"> 9 @{ 10 List<Team> list = ViewBag.TeamList as List<Team>; 11 foreach (var item in list) 12 { 13 <option value="@item.Id">@item.TeamName</option> 14 } 15 } 16 17 </select> 18 </div> 19 </div> 20 <div class="layui-form-item"> 21 <label class="layui-form-label">姓名 </label> 22 <div class="layui-input-block"> 23 <input type="text" name="MembersName" placeholder="请输入姓名" autocomplete="off" class="layui-input"> 24 </div> 25 </div> 26 <div class="layui-form-item"> 27 <label class="layui-form-label">球衣号码</label> 28 <div class="layui-input-inline"> 29 <input type="text" name="JerseyNumber" placeholder="请输入球衣号码" autocomplete="off" class="layui-input"> 30 </div> 31 <div class="layui-form-mid layui-word-aux">请输入数字</div> 32 </div> 33 34 <div class="layui-form-item"> 35 <label class="layui-form-label">请选择位置</label> 36 <div class="layui-input-block"> 37 <select name="Position"> 38 @{ 39 List<Position> positionList = ViewBag.PositionList as List<Position>; 40 foreach (var item in positionList) 41 { 42 <option value="@item._Position">@item._Position</option> 43 } 44 } 45 46 </select> 47 </div> 48 </div> 49 <div class="layui-form-item"> 50 <label class="layui-form-label">年龄 </label> 51 <div class="layui-input-block"> 52 <input type="text" name="Age" placeholder="请输入年龄" autocomplete="off" class="layui-input"> 53 </div> 54 </div> 55 <div class="layui-form-item"> 56 <label class="layui-form-label">身高 </label> 57 <div class="layui-input-block"> 58 <input type="text" name="Stature" placeholder="请输入身高(CM)" autocomplete="off" class="layui-input"> 59 </div> 60 </div> 61 <div class="layui-form-item"> 62 <div class="layui-input-block"> 63 <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button> 64 <button type="reset" class="layui-btn layui-btn-primary">重置</button> 65 </div> 66 </div> 67 </form> 68 69 70 <script> 71 layui.use(‘form‘, function () { 72 var form = layui.form; 73 74 //监听提交 75 form.on(‘submit(formDemo)‘, function (data) { 76 if (data.field.TeamId == null || data.field.TeamId == 0) { 77 layer.msg("请选择所属队伍", { icon: 2 }); 78 return false; 79 } 80 else if (data.field.MembersName == "" || data.field.MembersName == null) { 81 layer.msg("姓名不能为空", { icon: 2 }); 82 return false; 83 } 84 else if (data.field.JerseyNumber <= 0) { 85 layer.msg("球衣号码不能小于0", { icon: 2 }); 86 return false; 87 } 88 else if (data.field.JerseyNumber == null) { 89 layer.msg("球衣号码不能为空", { icon: 2 }); 90 return false; 91 } 92 else if (data.field.Position == "" || data.field.Position == null) { 93 layer.msg("位置不能为空", { icon: 2 }); 94 return false; 95 } 96 else 97 { 98 layer.msg("添加成功", { icon: 1 }); 99 100 } 101 102 }); 103 }); 104 </script>
ScoreingController
计分控制器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MVCVolleyball.Controllers 8 { 9 public class ScoreingController : BaseController 10 { 11 #region 选择比赛双方的队伍 12 /// <summary> 13 /// 选择比赛双方的队伍 14 /// </summary> 15 /// <returns></returns> 16 public ActionResult Index() 17 { 18 List<Team> teamList = Team.Fetch(""); 19 ViewBag.TeamList = teamList; 20 return View(); 21 } 22 #endregion 23 24 #region 添加总比分记录 25 /// <summary> 26 /// 添加总比分记录 27 /// </summary> 28 /// <param name="HomeTeamId"></param> 29 /// <param name="GuestTeamId"></param> 30 /// <returns></returns> 31 [HttpPost] 32 public ActionResult ScoreOne(int HomeTeamId, int GuestTeamId) 33 { 34 CompetitionScore model = new CompetitionScore() 35 { 36 GuestTeamId = GuestTeamId, 37 HomeTeamId = HomeTeamId, 38 GuestTeamScore = 0, 39 HomeTeamScore = 0 40 }; 41 object Id = db.Insert(model); 42 43 return Json("/Scoreing/ScoreTwo?Id=" + (int)Id + "&HomeTeamId=" + HomeTeamId + "&GuestTeamId=" + GuestTeamId + ""); 44 } 45 #endregion 46 47 #region 添加详细比分,精确到选手 48 /// <summary> 49 /// 添加详细比分,精确到选手 50 /// </summary> 51 /// <param name="Id"></param> 52 /// <param name="HomeTeamId"></param> 53 /// <param name="GuestTeamId"></param> 54 /// <returns></returns> 55 [HttpGet] 56 public ActionResult ScoreTwo(int Id, int HomeTeamId, int GuestTeamId) 57 { 58 ViewBag.Id = Id; 59 ViewBag.HomeMemberList = TeamMember.Fetch("where TeamId=" + HomeTeamId + ""); 60 ViewBag.GuestMemberList = TeamMember.Fetch("where TeamId=" + GuestTeamId + ""); 61 ViewBag.HomeTeamName = Team.FirstOrDefault("where Id=" + HomeTeamId + "").TeamName; 62 ViewBag.GuestTeamName = Team.FirstOrDefault("where Id=" + GuestTeamId + "").TeamName; 63 ViewBag.HomeTeamId = HomeTeamId; 64 ViewBag.GuestTeamId = GuestTeamId; 65 return View(); 66 } 67 [HttpPost] 68 public ActionResult ScoreTwo(DetailScore model) 69 { 70 db.Insert(model); 71 return Json(""); 72 } 73 #endregion 74 75 #region 添加回合比分,以及对总比分进行更新 76 /// <summary> 77 /// 添加回合比分,以及对总比分进行更新 78 /// </summary> 79 /// <param name="CompetitionId"></param> 80 /// <param name="Innings"></param> 81 /// <param name="HomeTeamId"></param> 82 /// <param name="GuestTeamId"></param> 83 /// <param name="HomeTeamScore"></param> 84 /// <param name="GuestTeamScore"></param> 85 /// <returns></returns> 86 [HttpPost] 87 public ActionResult HuiHeScore(int CompetitionId, int Innings, int HomeTeamId, int GuestTeamId, int HomeTeamScore, int GuestTeamScore) 88 { 89 string msg = "ok"; 90 CompetitionScore competitionScoreModel = CompetitionScore.Single(CompetitionId); 91 if (HomeTeamScore > GuestTeamScore) 92 { 93 competitionScoreModel.HomeTeamScore++; 94 if (competitionScoreModel.HomeTeamScore == 3) 95 { 96 msg = "/Home/Index"; 97 } 98 db.Update(competitionScoreModel); 99 } 100 else 101 { 102 competitionScoreModel.GuestTeamScore++; 103 if (competitionScoreModel.GuestTeamScore == 3) 104 { 105 msg = "/Home/Index"; 106 } 107 db.Update(competitionScoreModel); 108 } 109 TotalScore model = new TotalScore() 110 { 111 CompetitionId = CompetitionId, 112 GuestTeamId = GuestTeamId, 113 GuestTeamScore = GuestTeamScore, 114 HomeTeamId = HomeTeamId, 115 HomeTeamScore = HomeTeamScore, 116 Innings = Innings 117 }; 118 db.Insert(model); 119 return Json(msg); 120 } 121 #endregion 122 } 123 }
ScoreingController的视图
依旧是两个,第一个视图选择队伍,第二个进入计分板视图进行计分
选择队伍
1 @{ 2 ViewBag.Title = "计分板"; 3 } 4 <fieldset class="layui-elem-field " id="choose"> 5 <legend>选择队伍</legend> 6 <div class="layui-field-box"> 7 <div class="layui-form"> 8 <div class="layui-form-item"> 9 <label class="layui-form-label" style="margin-left:16%;">主队</label> 10 <div class="layui-input-inline"> 11 <select name="homeTeam" id="homeTeam"> 12 @{ 13 List<Team> list = ViewBag.TeamList as List<Team>; 14 foreach (var item in list) 15 { 16 <option value="@item.Id">@item.TeamName</option> 17 } 18 19 } 20 </select> 21 </div> 22 <label class="layui-form-label">客队</label> 23 <div class="layui-input-inline"> 24 <select name="guestTeam" id="guestTeam"> 25 @{ 26 foreach (var item in list) 27 { 28 <option value="@item.Id">@item.TeamName</option> 29 } 30 31 } 32 </select> 33 </div> 34 </div> 35 <div class="layui-form-item"> 36 <div class="layui-input-block"> 37 <button class="layui-btn" id="btn" style="margin-left:37%;">确定</button> 38 </div> 39 </div> 40 </div> 41 </div> 42 </fieldset> 43 <script> 44 //Demo 45 layui.use(‘form‘, function () { 46 var form = layui.form; 47 $("#btn").click(function () { 48 var homeTeamId = $("#homeTeam").val(); 49 var guestTeamId = $("#guestTeam").val(); 50 if (homeTeamId == guestTeamId) { 51 layer.msg("队伍不可相同", { icon: 2 }); 52 } 53 else { 54 //data.field.guestTeam; 55 $.post("/Scoreing/ScoreOne", 56 { 57 HomeTeamId: homeTeamId, 58 GuestTeamId: guestTeamId 59 }, function (data) { 60 window.location =data; 61 }); 62 63 } 64 }); 65 }); 66 </script>
计分板
1 @model DetailScore 2 @{ 3 ViewBag.Title = "计分板"; 4 } 5 @using (Ajax.BeginForm(new AjaxOptions 6 { 7 Url = "/Scoreing/ScoreTwo", 8 HttpMethod = "Post", 9 OnSuccess = "success", 10 OnBegin = "" 11 //Confirm= "isOK" 12 })) 13 { <fieldset class="layui-elem-field "> 14 <legend>计分板</legend> 15 <div class="layui-field-box"> 16 <div class="layui-form"> 17 <div class="layui-form-item"> 18 <input type="hidden" value="@ViewBag.Id" id="CompetitionId" name="CompetitionId" /> 19 <input type="hidden" value="@ViewBag.HomeTeamId" id="HomeTeamId" name="HomeTeamId" /> 20 <input type="hidden" value="@ViewBag.GuestTeamId" id="GuestTeamId" name="GuestTeamId" /> 21 <input type="hidden" value="1" id="Innings" name="Innings" /> 22 <input type="hidden" name="HomeTeamDetailScore" id="InputHomeTeamDetailScore" value="0"> 23 <input type="hidden" name="GuestTeamDetailScore" id="InputGuestTeamDetailScore" value="0"> 24 <label class="layui-form-label" style="margin-left:42%;font-size:24px; color:#FF5722;" id="inningsStr">第1局</label> 25 </div> 26 <div class="layui-form-item" style="width:300px;height:100px;margin-left:23%;font-size:18px; color:#FF5722;"> 27 <label class="layui-form-label" id="HomeTeamName">@ViewBag.HomeTeamName</label> 28 <label class="layui-form-label" id="HomeTeamDetailScore">0</label> 29 30 31 </div> 32 33 <div class="layui-form-item" style="width:300px;height:100px;float:right;margin-top:-115px; margin-right:25%;font-size:18px; color:#FF5722;"> 34 35 <label class="layui-form-label" id="GuestTeamDetailScore">0</label> 36 <label class="layui-form-label" id="GuestTeamName">@ViewBag.GuestTeamName</label> 37 38 </div> 39 <div class="layui-form-item"> 40 <div class="layui-input-block"> 41 <input type="button" class="layui-btn" id="homeTeamScorebtn" style="margin-left:30%;" value="主队得分" /> 42 <input type="button" class="layui-btn" id="guestTeamScorebtn" style="margin-left:10%;" value="客队得分" /> 43 </div> 44 </div> 45 46 47 @*//-----------------------------------------------------------------------主队得分选择队员面板*@ 48 49 <div class="layui-form" id="homeTeamScorediv" style="margin-top:30px;margin-left:36%;"> 50 <div class="layui-form-item"> 51 <label class="layui-form-label">得分队员</label> 52 <div class="layui-input-inline"> 53 <select name="HomeTeamMemberId" id="HomeTeamMemberId"> 54 <option value="0" selected>请选择队员</option> 55 @{ 56 List<TeamMember> homeMemberList = ViewBag.HomeMemberList as List<TeamMember>; 57 foreach (var item in homeMemberList) 58 { 59 <option value="@item.Id">@item.MembersName</option> 60 } 61 } 62 </select> 63 </div> 64 </div> 65 <div class="layui-form-item"> 66 <label class="layui-form-label">得分详情</label> 67 <div class="layui-input-inline"> 68 <input type="text" name="HomeTeamDetail" id="HomeTeamDetail" placeholder="请输入得分详情" autocomplete="off" class="layui-input"> 69 </div> 70 </div> 71 <div class="layui-form-item"> 72 <div class="layui-input-block"> 73 <input type="submit" class="layui-btn" value="确定" id="HomeSubmit" /> 74 </div> 75 </div> 76 77 </div> 78 @*//----------------------------------------------------------------------客队得分选择队员面板-*@ 79 <div class="layui-form" id="guestTeamScorediv" style="margin-top:30px;margin-left:36%;"> 80 <div class="layui-form-item"> 81 <label class="layui-form-label">得分队员</label> 82 <div class="layui-input-inline" id="guestTeamMemberDiv"> 83 <select name="GuestTeamMemberId" id="GuestTeamMemberId"> 84 <option value="0" selected>请选择队员</option> 85 @{ 86 List<TeamMember> guestMemberList = ViewBag.GuestMemberList as List<TeamMember>; 87 foreach (var item in guestMemberList) 88 { 89 <option value="@item.Id">@item.MembersName</option> 90 } 91 } 92 </select> 93 </div> 94 </div> 95 <div class="layui-form-item"> 96 <label class="layui-form-label">得分详情</label> 97 <div class="layui-input-inline"> 98 <input type="text" name="GuestTeamDetail" id="GuestTeamDetail" placeholder="请输入得分详情" autocomplete="off" class="layui-input"> 99 </div> 100 </div> 101 <div class="layui-form-item"> 102 <div class="layui-input-block"> 103 <input type="submit" class="layui-btn" value="确定" id="GuestSubmit"/> 104 </div> 105 </div> 106 </div> 107 </div> 108 </div> 109 </fieldset>} 110 111 112 <script> 113 layui.use(‘form‘, function () { 114 var form = layui.form; 115 }); 116 $("#homeTeamScorediv").hide(); 117 $("#guestTeamScorediv").hide(); 118 $("#homeTeamScorebtn").click(function () { 119 $("#guestTeamScorediv").fadeOut(); 120 $("#homeTeamScorediv").show(); 121 var homeScore = $("#InputHomeTeamDetailScore").val(); 122 homeScore++; 123 $("#HomeTeamDetailScore").html(homeScore); 124 125 }); 126 $("#guestTeamScorebtn").click(function () { 127 $("#homeTeamScorediv").fadeOut(); 128 $("#guestTeamScorediv").show(); 129 var guestScore = $("#InputGuestTeamDetailScore").val(); 130 guestScore++; 131 $("#GuestTeamDetailScore").html(guestScore); 132 }); 133 $("#HomeSubmit").click(function () { 134 $("#homeTeamScorediv").fadeOut(); 135 var homeScore = $("#InputHomeTeamDetailScore").val(); 136 homeScore++; 137 var guestScore = $("#InputGuestTeamDetailScore").val(); 138 $("#InputHomeTeamDetailScore").val(homeScore); 139 $("#GuestTeamMemberId").val(0); 140 $("#GuestTeamDetail").val(""); 141 victory(homeScore, guestScore); 142 }); 143 $("#GuestSubmit").click(function () { 144 $("#guestTeamScorediv").fadeOut(); 145 var guestScore = $("#InputGuestTeamDetailScore").val(); 146 guestScore++; 147 var homeScore = $("#InputHomeTeamDetailScore").val(); 148 $("#InputGuestTeamDetailScore").val(guestScore); 149 $("#HomeTeamMemberId").val(0); 150 $("#HomeTeamDetail").val(""); 151 victory(homeScore, guestScore); 152 }); 153 function success(data) { 154 //if (data == "添加成功") { 155 // layer.msg(data, { icon: 1 }); 156 // setTimeout("window.location = ‘/Teams/GetTeams‘", 2000); 157 //} 158 //else { 159 // layer.msg(data, { icon: 2 }); 160 //} 161 } 162 function victory( homeScore, guestScore) 163 { 164 165 var Innings = $("#Innings").val(); 166 var HomeTeamId = $("#HomeTeamId").val(); 167 var GuestTeamId = $("#GuestTeamId").val(); 168 var CompetitionId = $("#CompetitionId").val(); 169 if (Innings < 5) { 170 if ((parseInt(homeScore) >= 24 || parseInt(guestScore)>=24) && (Math.abs(homeScore - guestScore) >= 2)) 171 { 172 layer.msg("第" + Innings + "局结束", { icon: 1 }); 173 $.post("/Scoreing/HuiHeScore/", 174 { 175 CompetitionId: CompetitionId, 176 Innings: Innings, 177 HomeTeamId: HomeTeamId, 178 GuestTeamId: GuestTeamId, 179 HomeTeamScore: homeScore, 180 GuestTeamScore: guestScore 181 182 }, function (data) 183 { 184 if (data != "ok") { 185 layer.msg("比赛结束", { icon: 1 }); 186 setTimeout("window.location = ‘"+data+"‘", 2000); 187 } 188 else 189 { 190 Innings++; 191 $("#Innings").val(Innings); 192 $("#inningsStr").html("第" + Innings + "局"); 193 $("#InputHomeTeamDetailScore").val(0); 194 $("#InputGuestTeamDetailScore").val(0); 195 $("#GuestTeamDetailScore").html(0); 196 $("#HomeTeamDetailScore").html(0); 197 198 } 199 }); 200 201 } 202 } 203 else if (Innings == 5) 204 { 205 if ((parseInt(homeScore) >= 15 || parseInt(guestScore) >= 15) && (Math.abs(homeScore - guestScore) >= 2)) 206 { 207 layer.msg("第" + Innings + "局结束", { icon: 1 }); 208 $.post("/Scoreing/HuiHeScore/", 209 { 210 CompetitionId: CompetitionId, 211 Innings: Innings, 212 HomeTeamId: HomeTeamId, 213 GuestTeamId: GuestTeamId, 214 HomeTeamScore: homeScore, 215 GuestTeamScore: guestScore 216 217 }, function (data) { 218 if (data != "ok") { 219 layer.msg("比赛结束", { icon: 1 }); 220 setTimeout("window.location = ‘" + data + "‘", 2000); 221 } 222 }); 223 } 224 225 226 } 227 228 } 229 </script>
另外贴出T4模板以及Petapoco的代码
都是自动生成的
T4模板
1 <#@ include file="PetaPoco.Core.ttinclude" #> 2 <# 3 // Settings 4 ConnectionStringName = "Volleyball"; // Uses last connection string in config if not specified 5 Namespace = "MVCVolleyball"; 6 RepoName = ""; 7 GenerateOperations = true; 8 GeneratePocos = true; 9 GenerateCommon = true; 10 ClassPrefix = ""; 11 ClassSuffix = ""; 12 TrackModifiedColumns = false; 13 ExplicitColumns = true; 14 ExcludePrefix = new string[] {}; // Exclude tables by prefix. 15 16 // Read schema 17 var tables = LoadTables(); 18 19 20 /* 21 // Tweak Schema 22 tables["tablename"].Ignore = true; // To ignore a table 23 tables["tablename"].ClassName = "newname"; // To change the class name of a table 24 tables["tablename"]["columnname"].Ignore = true; // To ignore a column 25 tables["tablename"]["columnname"].PropertyName="newname"; // To change the property name of a column 26 tables["tablename"]["columnname"].PropertyType="bool"; // To change the property type of a column 27 */ 28 29 // Generate output 30 if (tables.Count>0) 31 { 32 #> 33 <#@ include file="PetaPoco.Generator.ttinclude" #> 34 <# } #>
1 // This file was automatically generated by the PetaPoco T4 Template 2 // Do not make changes directly to this file - edit the template instead 3 // 4 // The following connection settings were used to generate this file 5 // 6 // Connection String Name: `Volleyball` 7 // Provider: `System.Data.SqlClient` 8 // Connection String: `Data Source=COLORFUL-PC\SQLEXPRESS;Initial Catalog=Volleyball;Integrated Security=True` 9 // Schema: `` 10 // Include Views: `False` 11 12 using System; 13 using System.Collections.Generic; 14 using System.Linq; 15 using System.Web; 16 using PetaPoco; 17 18 namespace MVCVolleyball 19 { 20 public partial class VolleyballDB : Database 21 { 22 public VolleyballDB() 23 : base("Volleyball") 24 { 25 CommonConstruct(); 26 } 27 28 public VolleyballDB(string connectionStringName) 29 : base(connectionStringName) 30 { 31 CommonConstruct(); 32 } 33 34 partial void CommonConstruct(); 35 36 public interface IFactory 37 { 38 VolleyballDB GetInstance(); 39 } 40 41 public static IFactory Factory { get; set; } 42 public static VolleyballDB GetInstance() 43 { 44 if (_instance!=null) 45 return _instance; 46 47 if (Factory!=null) 48 return Factory.GetInstance(); 49 else 50 return new VolleyballDB(); 51 } 52 53 [ThreadStatic] static VolleyballDB _instance; 54 55 public override void OnBeginTransaction() 56 { 57 if (_instance==null) 58 _instance=this; 59 } 60 61 public override void OnEndTransaction() 62 { 63 if (_instance==this) 64 _instance=null; 65 } 66 67 public class Record<T> where T:new() 68 { 69 public static VolleyballDB repo { get { return VolleyballDB.GetInstance(); } } 70 public bool IsNew() { return repo.IsNew(this); } 71 public object Insert() { return repo.Insert(this); } 72 public void Save() { repo.Save(this); } 73 public int Update() { return repo.Update(this); } 74 public int Update(IEnumerable<string> columns) { return repo.Update(this, columns); } 75 public static int Update(string sql, params object[] args) { return repo.Update<T>(sql, args); } 76 public static int Update(Sql sql) { return repo.Update<T>(sql); } 77 public int Delete() { return repo.Delete(this); } 78 public static int Delete(string sql, params object[] args) { return repo.Delete<T>(sql, args); } 79 public static int Delete(Sql sql) { return repo.Delete<T>(sql); } 80 public static int Delete(object primaryKey) { return repo.Delete<T>(primaryKey); } 81 public static bool Exists(object primaryKey) { return repo.Exists<T>(primaryKey); } 82 public static bool Exists(string sql, params object[] args) { return repo.Exists<T>(sql, args); } 83 public static T SingleOrDefault(object primaryKey) { return repo.SingleOrDefault<T>(primaryKey); } 84 public static T SingleOrDefault(string sql, params object[] args) { return repo.SingleOrDefault<T>(sql, args); } 85 public static T SingleOrDefault(Sql sql) { return repo.SingleOrDefault<T>(sql); } 86 public static T FirstOrDefault(string sql, params object[] args) { return repo.FirstOrDefault<T>(sql, args); } 87 public static T FirstOrDefault(Sql sql) { return repo.FirstOrDefault<T>(sql); } 88 public static T Single(object primaryKey) { return repo.Single<T>(primaryKey); } 89 public static T Single(string sql, params object[] args) { return repo.Single<T>(sql, args); } 90 public static T Single(Sql sql) { return repo.Single<T>(sql); } 91 public static T First(string sql, params object[] args) { return repo.First<T>(sql, args); } 92 public static T First(Sql sql) { return repo.First<T>(sql); } 93 public static List<T> Fetch(string sql, params object[] args) { return repo.Fetch<T>(sql, args); } 94 public static List<T> Fetch(Sql sql) { return repo.Fetch<T>(sql); } 95 public static List<T> Fetch(long page, long itemsPerPage, string sql, params object[] args) { return repo.Fetch<T>(page, itemsPerPage, sql, args); } 96 public static List<T> Fetch(long page, long itemsPerPage, Sql sql) { return repo.Fetch<T>(page, itemsPerPage, sql); } 97 public static List<T> SkipTake(long skip, long take, string sql, params object[] args) { return repo.SkipTake<T>(skip, take, sql, args); } 98 public static List<T> SkipTake(long skip, long take, Sql sql) { return repo.SkipTake<T>(skip, take, sql); } 99 public static Page<T> Page(long page, long itemsPerPage, string sql, params object[] args) { return repo.Page<T>(page, itemsPerPage, sql, args); } 100 public static Page<T> Page(long page, long itemsPerPage, Sql sql) { return repo.Page<T>(page, itemsPerPage, sql); } 101 public static IEnumerable<T> Query(string sql, params object[] args) { return repo.Query<T>(sql, args); } 102 public static IEnumerable<T> Query(Sql sql) { return repo.Query<T>(sql); } 103 } 104 } 105 106 107 108 [TableName("dbo.CompetitionScore")] 109 [PrimaryKey("Id")] 110 [ExplicitColumns] 111 public partial class CompetitionScore : VolleyballDB.Record<CompetitionScore> 112 { 113 [Column] public int Id { get; set; } 114 [Column] public int? HomeTeamId { get; set; } 115 [Column] public int? GuestTeamId { get; set; } 116 [Column] public int? HomeTeamScore { get; set; } 117 [Column] public int? GuestTeamScore { get; set; } 118 } 119 120 [TableName("dbo.DetailScore")] 121 [PrimaryKey("Id")] 122 [ExplicitColumns] 123 public partial class DetailScore : VolleyballDB.Record<DetailScore> 124 { 125 [Column] public int Id { get; set; } 126 [Column] public int? CompetitionId { get; set; } 127 [Column] public int? Innings { get; set; } 128 [Column] public int? HomeTeamId { get; set; } 129 [Column] public int? GuestTeamId { get; set; } 130 [Column] public int? HomeTeamDetailScore { get; set; } 131 [Column] public int? GuestTeamDetailScore { get; set; } 132 [Column] public string HomeTeamDetail { get; set; } 133 [Column] public string GuestTeamDetail { get; set; } 134 [Column] public int? HomeTeamMemberId { get; set; } 135 [Column] public int? GuestTeamMemberId { get; set; } 136 } 137 138 [TableName("dbo.Position")] 139 [PrimaryKey("Id")] 140 [ExplicitColumns] 141 public partial class Position : VolleyballDB.Record<Position> 142 { 143 [Column] public int Id { get; set; } 144 [Column("Position")] public string _Position { get; set; } 145 } 146 147 [TableName("dbo.Team")] 148 [PrimaryKey("Id")] 149 [ExplicitColumns] 150 public partial class Team : VolleyballDB.Record<Team> 151 { 152 [Column] public int Id { get; set; } 153 [Column] public string TeamName { get; set; } 154 } 155 156 [TableName("dbo.TeamMember")] 157 [PrimaryKey("Id")] 158 [ExplicitColumns] 159 public partial class TeamMember : VolleyballDB.Record<TeamMember> 160 { 161 [Column] public int Id { get; set; } 162 [Column] public int? TeamId { get; set; } 163 [Column] public string MembersName { get; set; } 164 [Column] public int? JerseyNumber { get; set; } 165 [Column] public string Position { get; set; } 166 [Column] public int? Age { get; set; } 167 [Column] public decimal? Stature { get; set; } 168 } 169 170 [TableName("dbo.TotalScore")] 171 [PrimaryKey("Id")] 172 [ExplicitColumns] 173 public partial class TotalScore : VolleyballDB.Record<TotalScore> 174 { 175 [Column] public int Id { get; set; } 176 [Column] public int? CompetitionId { get; set; } 177 [Column] public int? Innings { get; set; } 178 [Column] public int? HomeTeamId { get; set; } 179 [Column] public int? GuestTeamId { get; set; } 180 [Column] public int? HomeTeamScore { get; set; } 181 [Column] public int? GuestTeamScore { get; set; } 182 } 183 }
以上是关于排球计分程序重构(五中篇)的主要内容,如果未能解决你的问题,请参考以下文章