クエリ文字列を_Layout.cshtmlページに渡しました
<li>@Html.ActionLink("Shopping", "Index1", new { controller = "Shopping", UserID = "Admin1" })</li>
テキストボックスから値を更新するための更新ビューがあります
@using (Html.BeginForm("Update", "Shopping", new { id = Request.QueryString["UserID"] }, FormMethod.Post, new { id = "myForm" }))
{
@Html.Hidden("id", @Request.QueryString["UserID"] as string)
@Html.Hidden("productid", item.ProductID as string)
@Html.TextBox("qty", item.Quantity)
@Html.Hidden("unitrate", item.Rate)
<input type="submit" value="Update" />
}
初めての投稿で、私はこのようなURLを取得します
http://localhost:61110/Shopping/Index1?UserID=Admin1
しかし、2回目は、投稿後に次のようなURLを取得します
http://localhost:61110/Shopping/Index1
フォームが投稿されたときに常にクエリ文字列値を取得する方法。
私もこのmvc3を試しました:空のクエリ文字列を返すhtml.beginform検索
しかし、2回目の投稿でクエリ文字列を取得できませんでした
。
編集:アクションコードを更新
[HttpPost]
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",new { id = Request.QueryString["UserID"] });
}
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 [Table_name] Set Quantity='" + qty + "',Price='" + total + "' where [User ID]='" + id + "' and [Product ID]='" + productid + "'", con);
cmd.Parameters.AddWithValue("@total", total);
return cmd.ExecuteNonQuery();
}