2012-10-22 6 views
2

에 쿼리 문자열 반환 나는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" /> 
     } 

에서 값을 업데이트하는 업데이트보기를 _Layout.cshtml 페이지 내 쿼리 문자열을 통과 처음 게시물, 난이 같은 URL을 얻을

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

는 그러나 두 번째, 포스트 후에 나는이 양식을 게시 할 때이

http://localhost:61110/Shopping/Index1 

같은 URL이 어떻게 항상 쿼리 문자열 값을 얻을 수.
나는이 mvc3: html.beginform search returning empty querystring

을 시도하지만 두 번째 포스트
어떤 제안에 쿼리 문자열을 얻을 couldnt한다.

편집 : 업데이트 작업 코드

[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

'업데이트'액션 코드를 게시 할 수 있습니까? 게시물의 URL이 어떻게되어야합니까? 'http : // localhost : 61110/Shopping/Index1? UserID = Admin1'과 같은가? 그래서'UserID = Admin1' 부분을 유지하고 싶습니까? – nemesv

+0

쿼리 문자열 http : // localhost : 61110/Shopping/Index1? UserID = Admin1을 사용하여 게시 할 URL이 필요합니다. – kk1076

답변

2

당신은뿐만 아니라 업데이트의 RedirectToAction에 new { id = Request.QueryString["UserID"] }를 추가해야합니다.

+0

RedirectToAction 메소드의 값을 전달했습니다. 이 값은 여기에서 볼 때 null입니다. @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