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

2021-07-14 17:06

阅读:798

标签:ssi   程序   lis   ==   turn   scores   解释   amt   table   

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

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

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

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

            //将十二个队员全部插入数据库 总共有十二个文本框 执行
            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;

            //将十二个队员全部插入数据库 总共有十二个文本框 执行
            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 = SqlHelper.GetAllPersonA();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List items = new List();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;


            StringBuilder sb = new StringBuilder();
            sb.Append("");
            for (int i = 0; i )
            {
                sb.AppendFormat(" ", i, personA[i].id);
            }
            sb.Append("");
            ViewBag.DropDownListA = sb.ToString();
            string[] jiQius = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1 = new StringBuilder();
            sb1.Append("");
            for (int i = 0; i )
            {
                sb1.AppendFormat(" ", i, jiQius[i]);
            }
            sb1.Append("");
            ViewBag.DropDownListJiQiu = sb1.ToString();

            StringBuilder sb2 = new StringBuilder();
            sb2.Append("");

            sb2.AppendFormat(" ", 1, "");
            sb2.AppendFormat(" ", 2, "");

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

            //B队开始

            List personB = SqlHelper.GetAllPersonB();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List items = new List();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sbB = new StringBuilder();
            sbB.Append("");
            for (int i = 0; i )
            {
                sbB.AppendFormat(" ", i, personB[i].id);
            }
            sbB.Append("");
            ViewBag.DropDownListB = sbB.ToString();
            string[] jiQiusB = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1B = new StringBuilder();
            sb1B.Append("");
            for (int i = 0; i )
            {
                sb1B.AppendFormat(" ", i, jiQiusB[i]);
            }
            sb1B.Append("");
            ViewBag.DropDownListJiQiuB = sb1B.ToString();

            StringBuilder sb2B = new StringBuilder();
            sb2B.Append("");

            sb2B.AppendFormat(" ", 1, "");
            sb2B.AppendFormat(" ", 2, "");

            sb2B.Append("");
            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局分//        if (juA >= 3 && juB //        {
            //            //获胜  把文本框里的值原样返回
            //            ViewBag.dangQianA = dangQianA.ToString();
            //            ViewBag.dangQianB = dangQianB.ToString();
            //            ViewBag.juA = juA.ToString();
            //            ViewBag.juB = juB.ToString();

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

            //        }

            //        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 = SqlHelper.GetAllPersonA();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List items = new List();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            for (int i = 0; i )
            {
                sb.AppendFormat(" ", i, personA[i].id);
            }
            sb.Append("");
            ViewBag.DropDownListA = sb.ToString();
            string[] jiQius = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1 = new StringBuilder();
            sb1.Append("");
            for (int i = 0; i )
            {
                sb1.AppendFormat(" ", i, jiQius[i]);
            }
            sb1.Append("");
            ViewBag.DropDownListJiQiu = sb1.ToString();

            StringBuilder sb2 = new StringBuilder();
            sb2.Append("");

            sb2.AppendFormat(" ", 1, "");
            sb2.AppendFormat(" ", 2, "");

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

            //B队开始

            List personB = SqlHelper.GetAllPersonB();
            //ViewData["PersonA"] = new SelectList(personA, "id","name");
            //List items = new List();
            //items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
            //items.Add(new SelectListItem { Text = "Jade", Value = "28" });
            //items.Add(new SelectListItem { Text = "Yao", Value = "24" });
            //this.ViewData["list"] = personA;
            //this.ViewData["selected"] = 24;
            StringBuilder sbB = new StringBuilder();
            sbB.Append("");
            for (int i = 0; i )
            {
                sbB.AppendFormat(" ", i, personB[i].id);
            }
            sbB.Append("");
            ViewBag.DropDownListB = sbB.ToString();
            string[] jiQiusB = { "拦网", "发球", "扣球", "抹球", "一传" };
            StringBuilder sb1B = new StringBuilder();
            sb1B.Append("");
            for (int i = 0; i )
            {
                sb1B.AppendFormat(" ", i, jiQiusB[i]);
            }
            sb1B.Append("");
            ViewBag.DropDownListJiQiuB = sb1B.ToString();

            StringBuilder sb2B = new StringBuilder();
            sb2B.Append("");

            sb2B.AppendFormat(" ", 1, "");
            sb2B.AppendFormat(" ", 2, "");

            sb2B.Append("");
            ViewBag.DropDownListIsFenB = sb2B.ToString();

            //查询数据库里所有的内容并用分布视图显示
            List scoreA = new List();
            scoreA = SqlHelper.GetAllScoreA();
            StringBuilder scoreSb = new StringBuilder();
            scoreSb.Append("");
            scoreSb.Append("");
            scoreSb.Append("");
            scoreSb.Append("");
            scoreSb.Append("");
            scoreSb.Append("");
            for (int i = 0; i )
            {
                scoreSb.Append("");
                scoreSb.AppendFormat("", scoreA[i].personId);
                scoreSb.AppendFormat("", scoreA[i].jiQiu);
                scoreSb.AppendFormat("", scoreA[i].isScore);
                scoreSb.Append("");
            }
            scoreSb.Append("
队员 击球状态 是否得分
{0} {0} {0}
"); ViewBag.selectA = scoreSb.ToString(); List scoreB = new List(); scoreB = SqlHelper.GetAllScoreB(); StringBuilder scoreBSb = new StringBuilder(); scoreBSb.Append(""); scoreBSb.Append(""); scoreBSb.Append(""); scoreBSb.Append(""); scoreBSb.Append(""); scoreBSb.Append(""); for (int i = 0; i ) { scoreBSb.Append(""); scoreBSb.AppendFormat("", scoreB[i].personId); scoreBSb.AppendFormat("", scoreB[i].jiQiu); scoreBSb.AppendFormat("", scoreB[i].isScore); scoreBSb.Append(""); } scoreBSb.Append("
队员 击球状态 是否得分
{0} {0} {0}
"); 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代码和一些解释

标签:ssi   程序   lis   ==   turn   scores   解释   amt   table   

原文地址:http://www.cnblogs.com/zyadmin/p/7074416.html


评论


亲,登录后才可以留言!