码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC 排球计分程序 (二)一些排球计分的基本实现方式

时间:2017-06-24 22:42:56      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:insert   ted   button   应用   mit   ini   begin   就会   rem   

一、连接数据库

新建一个MVC应用程序(基本)

建一个HomeController

为Index添加视图

 

@{

    ViewBag.Title = "Index";

}

 

<h2>Index</h2>

@using(Html.BeginForm("Indexa","Home")) {

 

  <b>id:</b>  @Html.TextBox("id",1);

  <b>name:</b> @Html.TextBox("name");

 

<button type="submit">提交</button>

}

 

在HomeController里新建一个Action 写入连接数据库的代码

public ActionResult Indexa()

        {

            SqlConnection conn = new SqlConnection();

            conn.ConnectionString = @"Data Source=.;Initial Catalog=paiqiu;Integrated Security=True";

            SqlCommand comm = new SqlCommand();

            comm.Connection = conn;

            string aa=Request["id"].ToString();

            int c = int.Parse(aa);

            string aaa = Request["name"].ToString();

         

            comm.CommandText = "insert into yifu (id,name) values ("+c+",‘"+aaa+"‘)";

            conn.Open();

            int a = comm.ExecuteNonQuery();

            conn.Close();

            if (a > 0)

            {

                ViewBag.S = "插入成功";

                ViewBag.J = "插入结果为";

                comm.CommandText = "select *from yifu";

                conn.Open();

                SqlDataReader dr = comm.ExecuteReader();

                if (dr.HasRows)

                {

                    ViewBag.a = "<table><tr><td>编号</td><td>名字</td></tr>";

                    while (dr.Read())

                    {

                        ViewBag.a += "<tr><td>" + dr["id"] + "</td><td>" + dr["name"] + "</td></tr>";

                    }

                }

                ViewBag.a += "</table>";

 

 

            }

            else

            {

                ViewBag.a += "插入失败";

                ViewBag.S = "";

                ViewBag.J = "";

            }

            return View();

        }

二 、 基本得分的实现方法

新建一个Action

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

为它添加视图

@{
    ViewBag.Title = "Indexb";
}

<h2>Indexb</h2>
@using (Html.BeginForm("Indexc", "Home"))
{ 
    string str=ViewBag.ccc;
    if (string.IsNullOrEmpty(str))
    {
        str = "0";
    }
    @Html.TextBox("number",str);
    <button type="submit">得分</button>
}

再添加一个Action

   

   public ActionResult Indexc()

        {

           int a=int.Parse( Request["number"]);



           if (a < 5)

           {

               a++;

           }

            ViewBag.ccc = a.ToString();

            return View("Indexb");

        }

这是这个排球计分程序的基本得分的技术实现

 

经过一系列判断就会是一个比较复杂的程序

@{

    ViewBag.Title = "Indexb";

}

 

<h2>Indexb</h2>

@using (Html.BeginForm("Indexc", "Home"))

{

    string str=ViewBag.ccc;

    if (string.IsNullOrEmpty(str))

    {

        str = "0";

    }

    @Html.TextBox("number",str);

    <buttontype="submit">得分</button>

}

再添加一个Action

      publicActionResult Indexc()

        {

           int a=int.Parse( Request["number"]);

 

           if (a < 5)

           {

               a++;

           }

            ViewBag.ccc = a.ToString();

            return View("Indexb");

        }

ASP.NET MVC 排球计分程序 (二)一些排球计分的基本实现方式

标签:insert   ted   button   应用   mit   ini   begin   就会   rem   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!