我正在尝试从文本框中获取文本更改值以在我的 Controller 中更新。
查看
@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>
并在我的 Controller 中进行更新
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
中传递了数量变量的文本框名称。但是当文本框值改变时,值不会传入其中。
编辑:
最初,数据库中文本框的值是 1。当我更改文本框值时,它不会更新,即使发布表单,也会更新相同的值。
最佳答案
您需要使用表单将值从 View 发送(POST)到 Controller 中。
这是一个粗略的例子:
@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>
}
关于c# - 如何在 MVC 中的 @Html.ActionLink 中获取更新的文本框更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12952439/