2

コントローラで更新するために、テキストボックスからtextchanged値を取得しようとしています。
意見

  @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>

そして、アップデート付きのマイコントローラーで

 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です。テキストボックスの値を変更すると、フォームが投稿されても更新されず、同じ値が更新されます。

4

1 に答える 1

2

フォームを使用して、ビューからコントローラーに値を送信(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>
}
于 2012-10-18T10:30:50.103 に答える