c# - 如何在 MVC 中的 @Html.ActionLink 中获取更新的文本框更改值

标签 c# asp.net asp.net-mvc-4

我正在尝试从文本框中获取文本更改值以在我的 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/

相关文章:

c# - 将结构数组从 C# 传递到 C++ DLL

asp.net-mvc-4 - 登录后 WebSecurity.CurrentUserName 和 User.Identity.Name 为空

c# - 如何在C#中设置下载超时

c# - 无法实现撤消/重做功能,我应该使用 Stack 吗?

c# - IIS 上的域托管

asp.net - 使用 ASP .Net MVC4 在 Web API 中处理集合内的集合

c# - 混合 Unity Web API 和 MVC4 UnityDependencyResolvers : No parameterless constructor defined for this object

c# - View 显示不同的值,然后在其模型中显示不同的值

c# - LINQ 中的匿名类型查询或普通查询

asp.net - 如何在 ASP.NET 中设置默认页面?