首页 > 代码库 > ASP.NET MVC 排球计分程序 (六)HomeController代码和一些解释

ASP.NET MVC 排球计分程序 (六)HomeController代码和一些解释

Index Action方法 (默认进入的主页)

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

AddPerson Action方法(单击主页提交 将交到AddPerson里来处理数据,并将队员 名字插入数据库)

 
 public ActionResult AddPerson()
        {
            //获取填入的衣服编号和名字 并将其写入数据库
            //编号idA1  nameA1
            SqlHelper.ClearBiaoRen();
            //插入B的idB1  nameB1..
            int numsA = 1;

            //将十二个队员全部插入数据库 总共有十二个文本框 执行<=12次的循环 插入值
            while (numsA <= 12)
            {
                //获取文本框名为idB1到idB12的值
                int idA = int.Parse(Request["idA" + numsA]);
                //获取文本框名为nameB1到nameB12的值
                string name = Request["nameA" + numsA];

                //排除name为空的情况
                if (string.IsNullOrEmpty(name))
                {
                    numsA++;
                    continue;
                }
                else
                {
                    //name不为空插入 idB1  nameA1等进数据库

                    string sql = "insert into PersonA values(" + idA + ",‘" + name + "‘)";
                    int a = SqlHelper.ExecuteNonQuery(sql, null);
                    if (a > 0)
                    {
                        ViewBag.IsSuccessA = "";
                    }
                    else
                    {
                        ViewBag.IsSuccessA = "A插入失败,未知错误";
                    }
                }
                numsA++;
            }
            //插入B的idB1  nameB1..
            int numsB = 1;

            //将十二个队员全部插入数据库 总共有十二个文本框 执行<=12次的循环 插入值
            while (numsB <= 12)
            {
                //获取文本框名为idB1到idB12的值
                int idB = int.Parse(Request["idB" + numsB]);
                //获取文本框名为nameB1到nameB12的值
                string name = Request["nameB" + numsB];

                //排除name为空的情况
                if (string.IsNullOrEmpty(name))
                {
                    numsB++;
                    continue;
                }
                else
                {
                    //name不为空插入 idB1  nameA1等进数据库

                    string sql = "insert into PersonB values(" + idB + ",‘" + name + "‘)";
                    int a = SqlHelper.ExecuteNonQuery(sql, null);
                    if (a > 0)
                    {
                        ViewBag.IsSuccessB = "";
                    }
                    else
                    {
                        ViewBag.IsSuccessB = "B插入失败,未知错误";
                    }
                }
                numsB++;
            }
            //string teamAName = Request["teamAName"];
            //string teamBName = Request["teamBName"];
            //获取teamA teamB文本框内的值
            ViewBag.teamAName = Request["teamAName"];
            ViewBag.teamBName = Request["teamBName"];
            //显示当前比分 局分都是0
            ViewBag.dangQianA = "0";
            ViewBag.dangQianB = "0";
            ViewBag.juA = "0";
            ViewBag.juB = "0";
            List<PersonA> personA = SqlHelper.GetAllPersonA();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List<SelectListItem> items = new List<SelectListItem>();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "http://www.mamicode.com/29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "http://www.mamicode.com/28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "http://www.mamicode.com/24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;


            StringBuilder sb = new StringBuilder();
            sb.Append("<select id=\"teamA\">");
            for (int i = 0; i < personA.Count; i++)
            {
                sb.AppendFormat(" <option value =http://www.mamicode.com/teamA{0}>{1}", i, personA[i].id);
            }
            sb.Append("</select>");
            ViewBag.DropDownListA = sb.ToString();
            string[] jiQius = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1 = new StringBuilder();
            sb1.Append("<select id=\"teamAJiQiu\">");
            for (int i = 0; i < jiQius.Length; i++)
            {
                sb1.AppendFormat(" <option value =http://www.mamicode.com/teamAJiQiu{0}>{1}", i, jiQius[i]);
            }
            sb1.Append("</select>");
            ViewBag.DropDownListJiQiu = sb1.ToString();

            StringBuilder sb2 = new StringBuilder();
            sb2.Append("<select id=\"teamAIsFen\">");

            sb2.AppendFormat(" <option value =http://www.mamicode.com/teamAIsFen{0}>{1}", 1, "");
            sb2.AppendFormat(" <option value =http://www.mamicode.com/teamAIsFen{0}>{1}", 2, "");

            sb2.Append("</select>");
            ViewBag.DropDownListIsFen = sb2.ToString();
            //A队结束

            //B队开始

            List<PersonB> personB = SqlHelper.GetAllPersonB();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List<SelectListItem> items = new List<SelectListItem>();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "http://www.mamicode.com/29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "http://www.mamicode.com/28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "http://www.mamicode.com/24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sbB = new StringBuilder();
            sbB.Append("<select id=\"teamB\">");
            for (int i = 0; i < personB.Count; i++)
            {
                sbB.AppendFormat(" <option value =http://www.mamicode.com/teamB{0}>{1}", i, personB[i].id);
            }
            sbB.Append("</select>");
            ViewBag.DropDownListB = sbB.ToString();
            string[] jiQiusB = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1B = new StringBuilder();
            sb1B.Append("<select id=\"teamBJiQiu\">");
            for (int i = 0; i < jiQiusB.Length; i++)
            {
                sb1B.AppendFormat(" <option value =http://www.mamicode.com/teamBJiQiu{0}>{1}", i, jiQiusB[i]);
            }
            sb1B.Append("</select>");
            ViewBag.DropDownListJiQiuB = sb1B.ToString();

            StringBuilder sb2B = new StringBuilder();
            sb2B.Append("<select id=\"teamBIsFen\">");

            sb2B.AppendFormat(" <option value =http://www.mamicode.com/teamBIsFen{0}>{1}", 1, "");
            sb2B.AppendFormat(" <option value =http://www.mamicode.com/teamBIsFen{0}>{1}", 2, "");

            sb2B.Append("</select>");
            ViewBag.DropDownListIsFenB = sb2B.ToString();
            return View("AddScore");
        }

这个Action主要写了插入名字到对应的PersonA  PersonB 数据库 

弹出新的界面应显示的一些内容 

如队名 根据自己在数据库中插入的内容而生成的下拉列表  队名的传递也可以使用Cookie 和session这里不做解释

ViewBag.名字 可以用来传递数据

在视图中用@VieWBag名字  可以获取到自己传递的值 详情可以了解一下Rezor语法等

Admin Action方法

public ActionResult Admin()
        {

            //获取文本框的值
            //获取是A队得分还是B队得分
            string teamMing = Request["teamMing"];
            //获取A队的名字
            string teamAName = Request["tA"];
            //获取B队的名字
            string teamBName = Request["tB"];
            //重新将名称写入文本框
            ViewBag.teamAName = teamAName;
            ViewBag.teamBName = teamBName;
            //获取是哪一个球员
            string teamYuan = Request["teamYuan"];
            //获取它是哪一种状态
            string teamTai = Request["teamTai"];
            //获取当前状态是否得分
            string teamIsFen = Request["teamIsFen"];
            //获取当前比分A队的值
            //int dangQianA;
            //if (string.IsNullOrEmpty(Request["dangQiangA"]))
            //{
            //    dangQianA = 0;
            //}
            //else 
            //{
            //    dangQianA = int.Parse(Request["dangQiangA"]);
            //}
            
            ////获取当前比分B队的值
            //int dangQianB;
            //if (string.IsNullOrEmpty(Request["dangQiangB"]))
            //{
            //    dangQianB = 0;
            //}
            //else
            //{
            //    dangQianB = int.Parse(Request["dangQiangB"]);
            //}
            ////获取局比分A
            //int juA;
            //if (string.IsNullOrEmpty(Request["juA"]))
            //{
            //    juA = 0;
            //}
            //else
            //{
            //    juA = int.Parse(Request["juA"]);
            //}
            ////获取局比分B
            //int juB;
            //if (string.IsNullOrEmpty(Request["juB"]))
            //{
            //    juB = 0;
            //}
            //else
            //{
            //    juB = int.Parse(Request["juB"]);
            //}
            if (teamMing == teamAName)
            {
            //    //0 0 1 0 2 0 3 0 
            //    //给A队加分
            //    if (teamIsFen == "是")
            //    {
            //        //0 1 2 3 4
            //        //如果A局分>=3 且局分B局分<2不加分

            //        if (juA >= 3 && juB < 2)
            //        {
            //            //获胜  把文本框里的值原样返回
            //            ViewBag.dangQianA = dangQianA.ToString();
            //            ViewBag.dangQianB = dangQianB.ToString();
            //            ViewBag.juA = juA.ToString();
            //            ViewBag.juB = juB.ToString();

            //        }
            //        //进入决胜局
            //        else if (juA >= 3 && juB <= 2)
            //        {

            //        }

            //        else
            //        {
            //            //如果加分后A>=25且>B则获胜
            //            if (dangQianA >= 25 && dangQianA >= dangQianB + 2)
            //            {
            //                //A获得胜利 加局分
            //                ViewBag.juA = juA++;
            //                //清空比分 
            //                ViewBag.dangQianA = "0";
            //                ViewBag.dangQianB = "0";
                            
            //                //当前比分A加1
            //                ViewBag.dangQianA = (dangQianA++).ToString();
            //            }
            //            else
            //            {
            //                //加分后不大于25的情况
            //                ViewBag.dangQianA = (dangQianA++).ToString();
            //            }
            //        }

            //    }
            //    else
            //    { 
            //        //等于否 则将当前分和局分原样返回
            //        ViewBag.dangQianA = dangQianA.ToString();
            //        ViewBag.dangQianB = dangQianB.ToString();
            //        ViewBag.juA = juA.ToString();
            //        ViewBag.juB = juB.ToString();
            //    }
                //无论是否都将其插入数据库
                string sql = "insert into ScoreA (personId,jiQiu,isScore) values(" + int.Parse(teamYuan) + ",‘" + teamTai + "‘,‘" + teamIsFen + "‘)";
                //插入数据库
                SqlHelper.ExecuteNonQuery(sql, null);
            }
            else
            {
                //给B队加分
                string sql = "insert into ScoreB (personId,jiQiu,isScore) values(" + int.Parse(teamYuan) + ",‘" + teamTai + "‘,‘" + teamIsFen + "‘)";
                //插入数据库
                SqlHelper.ExecuteNonQuery(sql, null);

            }
            //三个下拉列表出现
            List<PersonA> personA = SqlHelper.GetAllPersonA();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List<SelectListItem> items = new List<SelectListItem>();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "http://www.mamicode.com/29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "http://www.mamicode.com/28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "http://www.mamicode.com/24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sb = new StringBuilder();
            sb.Append("<select id=\"teamA\">");
            for (int i = 0; i < personA.Count; i++)
            {
                sb.AppendFormat(" <option value =http://www.mamicode.com/teamA{0}>{1}", i, personA[i].id);
            }
            sb.Append("</select>");
            ViewBag.DropDownListA = sb.ToString();
            string[] jiQius = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1 = new StringBuilder();
            sb1.Append("<select id=\"teamAJiQiu\">");
            for (int i = 0; i < jiQius.Length; i++)
            {
                sb1.AppendFormat(" <option value =http://www.mamicode.com/teamAJiQiu{0}>{1}", i, jiQius[i]);
            }
            sb1.Append("</select>");
            ViewBag.DropDownListJiQiu = sb1.ToString();

            StringBuilder sb2 = new StringBuilder();
            sb2.Append("<select id=\"teamAIsFen\">");

            sb2.AppendFormat(" <option value =http://www.mamicode.com/teamAIsFen{0}>{1}", 1, "");
            sb2.AppendFormat(" <option value =http://www.mamicode.com/teamAIsFen{0}>{1}", 2, "");

            sb2.Append("</select>");
            ViewBag.DropDownListIsFen = sb2.ToString();
            //A队结束

            //B队开始

            List<PersonB> personB = SqlHelper.GetAllPersonB();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List<SelectListItem> items = new List<SelectListItem>();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "http://www.mamicode.com/29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "http://www.mamicode.com/28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "http://www.mamicode.com/24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sbB = new StringBuilder();
            sbB.Append("<select id=\"teamB\">");
            for (int i = 0; i < personB.Count; i++)
            {
                sbB.AppendFormat(" <option value =http://www.mamicode.com/teamB{0}>{1}", i, personB[i].id);
            }
            sbB.Append("</select>");
            ViewBag.DropDownListB = sbB.ToString();
            string[] jiQiusB = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1B = new StringBuilder();
            sb1B.Append("<select id=\"teamBJiQiu\">");
            for (int i = 0; i < jiQiusB.Length; i++)
            {
                sb1B.AppendFormat(" <option value =http://www.mamicode.com/teamBJiQiu{0}>{1}", i, jiQiusB[i]);
            }
            sb1B.Append("</select>");
            ViewBag.DropDownListJiQiuB = sb1B.ToString();

            StringBuilder sb2B = new StringBuilder();
            sb2B.Append("<select id=\"teamBIsFen\">");

            sb2B.AppendFormat(" <option value =http://www.mamicode.com/teamBIsFen{0}>{1}", 1, "");
            sb2B.AppendFormat(" <option value =http://www.mamicode.com/teamBIsFen{0}>{1}", 2, "");

            sb2B.Append("</select>");
            ViewBag.DropDownListIsFenB = sb2B.ToString();

            //查询数据库里所有的内容并用分布视图显示
            List<ScoreA> scoreA = new List<ScoreA>();
            scoreA = SqlHelper.GetAllScoreA();
            StringBuilder scoreSb = new StringBuilder();
            scoreSb.Append("<table>");
            scoreSb.Append("<tr>");
            scoreSb.Append("<td>队员</td>");
            scoreSb.Append("<td>击球状态</td>");
            scoreSb.Append("<td>是否得分</td>");
            scoreSb.Append("</tr>");
            for (int i = 0; i < scoreA.Count; i++)
            {
                scoreSb.Append("<tr>");
                scoreSb.AppendFormat("<td>{0}</td>", scoreA[i].personId);
                scoreSb.AppendFormat("<td>{0}</td>", scoreA[i].jiQiu);
                scoreSb.AppendFormat("<td>{0}</td>", scoreA[i].isScore);
                scoreSb.Append("</tr>");
            }
            scoreSb.Append("</table>");
            ViewBag.selectA = scoreSb.ToString();

            List<ScoreB> scoreB = new List<ScoreB>();
            scoreB = SqlHelper.GetAllScoreB();
            StringBuilder scoreBSb = new StringBuilder();
            scoreBSb.Append("<table>");
            scoreBSb.Append("<tr>");
            scoreBSb.Append("<td>队员</td>");
            scoreBSb.Append("<td>击球状态</td>");
            scoreBSb.Append("<td>是否得分</td>");
            scoreBSb.Append("</tr>");
            for (int i = 0; i < scoreB.Count; i++)
            {
                scoreBSb.Append("<tr>");
                scoreBSb.AppendFormat("<td>{0}</td>", scoreB[i].personId);
                scoreBSb.AppendFormat("<td>{0}</td>", scoreB[i].jiQiu);
                scoreBSb.AppendFormat("<td>{0}</td>", scoreB[i].isScore);
                scoreBSb.Append("</tr>");
            }
            scoreBSb.Append("</table>");
            ViewBag.selectB = scoreBSb.ToString();
            return View("AddScore");
        }

 

AddScore Action

 public ActionResult AddScore()
        {


            return View();
        }

selectScore

 public ActionResult selectScore()
        {
            ViewBag.AMaxFen = SqlHelper.getAMaxScore();
            ViewBag.BMaxFen = SqlHelper.getBMaxScore();
            ViewBag.AFaQiu = SqlHelper.getAFaQiuMax(jiQiu.发球);
            ViewBag.BFaQiu = SqlHelper.getBFaQiuMax(jiQiu.发球);
            ViewBag.AYiChuan = SqlHelper.getAFaQiuMax(jiQiu.一传);
            ViewBag.BYiChuan = SqlHelper.getBFaQiuMax(jiQiu.一传);
            ViewBag.AKouQiu = SqlHelper.getAFaQiuMax(jiQiu.扣球);
            ViewBag.BKouQiu = SqlHelper.getBFaQiuMax(jiQiu.扣球);
            ViewBag.AMoQiu = SqlHelper.getAFaQiuMax(jiQiu.抹球);
            ViewBag.BMoQIu = SqlHelper.getBFaQiuMax(jiQiu.抹球);
            ViewBag.ALanWang = SqlHelper.getAFaQiuMax(jiQiu.拦网);
            ViewBag.BLanWang = SqlHelper.getBFaQiuMax(jiQiu.拦网);
            return View();
        }

下一篇将介绍对应视图的代码

 

ASP.NET MVC 排球计分程序 (六)HomeController代码和一些解释