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

adminyu http://www.cnblogs.com/zyadmin/p/7074190.html

一、连接数据库

新建一个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");

        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值