4月 02, 2014

MVC 用表單送資料給資料庫

首先是View
    @{Html.BeginForm("postdata", "home", FormMethod.Post);}
    @Html.Label("hh")<br>
    First name: <input id="textbox1" name="name1" type="text" value=""><br>
    Last name:  @Html.TextBox("name2", "", new {id="textbox2" })
    <br>
    <input id="Submit11" type="submit" value="submit" />
    @{Html.EndForm();}
再來是controll
        [HttpPost]
        public ActionResult postdata(int name1,int name2)
        {
            //ViewData["result"] = name1 + name2;
            //ViewBag.da = name1 + name2;
            int hh = name1 + name2;
            ty.see56(hh);
            return RedirectToAction("Index");
        }
接著是Model
 public int see56(int vlk)
        {

            SqlDataSource sds = new SqlDataSource();
            sds.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            sds.UpdateCommand = "update item set mp=@mp where id=1";
            sds.UpdateParameters.Add(new Parameter("mp",DbType.String,vlk.ToString()));
            sds.Update();
            
           
            return 0;
            //return dv.Table.Rows[2].ItemArray[1].ToString();
        }
大功告成!

沒有留言: