我正在尝试从文本框中获取文本变换值以在我的控制器中更新。
查看
@Html.TextBox("Quantity", item.Quantity)
<a href="@Url.Action("Update", "Shopping", new { id = Request.QueryString["UserID"], productid = item.ProductID, qty ="Quantity", unitrate = item.Rate })">
<img alt="updateitem" style="vertical-align: middle;" height="17px" src="~/Images/product_updates_image.png"
title="update" id="imgUpdate" />
</a>
,在我的控制器中使用update
public ActionResult Update(string id, string productid, int qty, decimal unitrate)
{
if (ModelState.IsValid)
{
int _records = UpdatePrice(id,productid,qty,unitrate);
if (_records > 0)
{
return RedirectToAction("Index1", "Shopping");
}
else
{
ModelState.AddModelError("","Can Not Update");
}
}
return View("Index1");
}
更新功能
public int UpdatePrice(string id,string productid, int qty, decimal unitrate)
{
con.Open();
var total = qty * unitrate;
SqlCommand cmd = new SqlCommand("Update [Cpecial_Shopping_Cart_tbl] Set Price='"+ total +"' where [User ID]='" + id + "' and [Product ID]='" + productid + "'", con);
cmd.Parameters.AddWithValue("@total", total);
return cmd.ExecuteNonQuery();
}
我传递了@Html.ActionLink
中数量变量的文本框名。但是,当更改文本框值时,未传递值。
编辑:
最初,来自db的文本框的值为1。当我更改文本框值时,即使在表单发布时,它也不会更新和相同的值。
您需要使用表单从视图中发送(post)值到控制器。
这是一个粗略的例子:
@using (Html.BeginForm("Update", "Shopping", FormMethod.Post, new { @id = "myHtmlForm" }))
{
@Html.Hidden("id", Request.QueryString["UserID"]);
@Html.Hidden("productid", item.ProductID)
@Html.Hidden("unitrate", item.Rate)
@Html.TextBox("qty", item.Quantity)
<a href="javascript:document.getElementById('myHtmlForm').submit();">
<img alt="updateitem" style="vertical-align: middle;" height="17px" src="~/Images/product_updates_image.png"
title="update" id="imgUpdate" />
</a>
}