2012-10-22 26 views
2

查詢字符串我通過我的查詢字符串中_Layout.cshtml頁FormMethod.Post返回在MVC4

<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" /> 
     } 

值在第一次發佈,我得到這樣的網址

http://localhost:61110/Shopping/Index1?UserID=Admin1 

但第二次,後後,我拿到它時的形式張貼這樣

http://localhost:61110/Shopping/Index1 

的URL如何在任何時候都得到查詢字符串值。
我也試過這個mvc3: html.beginform search returning empty querystring

但不可能得到第二次交
任何建議的查詢字符串。

編輯:更新操作代碼

[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(); 
    } 
+0

你可以發佈你的'更新'行動代碼?該帖子後的網址應該如何?像'http:// localhost:61110/Shopping/Index1?UserID = Admin1'?那麼你想保留'UserID = Admin1'部分嗎? – nemesv

+0

我需要的網址發佈querystring http:// localhost:61110/Shopping/Index1?UserID = Admin1 – kk1076

回答

2

您需要的new { id = Request.QueryString["UserID"] }添加到更新RedirectToAction爲好。

+0

我在RedirectToAction方法中傳遞了值。這裏的值是null。@ using(Html.BeginForm(「Update」,「Shopping」,new {id = Request.QueryString [「UserID」]},FormMethod.Post,new {id =「myForm 「})) – kk1076

+1

@ kk1076 - 上面發佈的代碼在Update方法中沒有RedirectToAction的路由值。另外,我剛剛意識到你正在使用id,你想'UserID = Request.QueryString [「UserID」];' –

+0

@MM - 現在檢查我的編輯。 – kk1076

相關問題