2014-12-29 55 views
0

我有下面的代碼發送在MVC4

 public ActionResult Item_Post() 
    { 
     List<Product> products=new List<Product>() ; 
     int? total=0; 
     HttpCookie cookie= Request.Cookies["myvalue"]; 
     if (Request.Cookies["myvalue"] != null) 
     { 
      int count = Request.Cookies["myvalue"].Values.Count; 
       var s = Request.Cookies["myvalue"].Value; 
       s = HttpUtility.UrlDecode(s ?? string.Empty); 
       string[] values = s.Split(',').Select(x => x.Trim()).ToArray();      
       for (int i = 1; i < values.Length; i++) 
       { 
        int id = Convert.ToInt32(values[i]); 
        Product product = db.Products.Single(x => x.Id == id);      
        total+=product.Price; 
        products.Add(product); 
       } 
       ViewBag.total = total;  
      TempData["products"]=products; 
     } 
     Session["prod"] = products; 
     return View("Buy", products); 
     //return RedirectToAction("Buy"); 
    } 

現在使用RedirectToAction列表時,我只用返回視圖(「買入」,產品),我得到的輸出和我想要的網址仍然是相同的更改網址,當我使用

return RedirectToAction("Buy", products); 

我收到錯誤,因爲我想發佈表單購買。參數在RedirectToAction中傳遞是否合適,或者是否需要其他參數。 這裏是行動

@model IEnumerable<Shop_Online.com.Models.Product> 
@{ 
ViewBag.Title = "Buy"; 
} 
@using (Html.BeginForm()) 
{ 
<div style="width: 860px; margin: 0 auto" class="main"> 
    <table border="1" style="font-family: Verdana; font-size: 13px"> 
     <tr style="background-color: #f2f2f2"> 
      <th colspan="4">ITEM</th> 
      <th>DELIEVERY DETAILS</th> 
      <th>QTY</th> 
      <th>SUB TOTAL</th> 
     </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td colspan="4" style="width: 46%"> 
        <table style="font-family: Verdana; font-size: 13px"> 
         <tr> 
          <td> 
           <img src="@Url.Content(item.Photo)" alt="Image" style="width:36px" /> 
          </td> 
          <td> 
           @Html.DisplayFor(x => item.Model_Name) 
          </td> 
         </tr> 
         <tr> 
          <td style="color: #ccc">30 days Replacement</td> 
         </tr> 
        </table> 
       </td> 
       <td style="width: 39%">Free Delievery Delivered in 2-3 business days.</td> 
       <td style="width: 5%">1</td> 
       <td style="width: 50%"><b>Rs. @Html.DisplayFor(x => item.Price)</b></td> 
      </tr> 
     } 
    </table> 
    <div style="width: 100%; height: 70px; background-color: #f2f2f2"> 
     <div style="width: 75%; height: 70px; float: left; font-family: Verdana; font-size: 13px"> 
     </div> 
     <div style="width: 25%; height: 70px; float: left; font-family: Verdana; padding-top: 20px; font-size: 13px"> 
      Estimated Price: <b>[email protected]</b> 
     </div> 
    </div> 
    <div class="order" style="width: 100%; height: 70px"> 
     <div class="left-order" style="width: 75%; height: 70px; float: left"></div> 
     <div class="left-order" style="width: 25%; float: left; height: 70px"> 
      <input type="button" value="PLACE ORDER" style="border: 1px solid #ec6723; width: 216px; cursor: pointer; height: 45px; color: #fff; background: -webkit-linear-gradient(top,#f77219 1%,#fec6a7 3%,#f77219 7%,#f75b16 100%)" onclick="return confirm('Successfull placed order')" /> 
     </div> 

    </div> 
</div> 
} 

現在我應該怎麼替換我的視圖中的下面的代碼如果我使用TempData的

@foreach(var item in Model) 
{ 
@Html.DisplayFor(model=>model.Name 
/some more code/ 
} 
+0

PLZ評論,如果有不清楚的地方 – User

+0

http://stackoverflow.com/questions/1257482/redirecttoaction-with-parameter – ssilas777

+0

@ ssilas777因爲我傳遞值 – User

回答

3

你不能在你的操作方法傳遞給RedirectToActionlist或任何model對象。因爲RedirectToAction導致HTTP 302 (Redirect)請求,這使瀏覽器調用GET請求的操作。

您應該使用TempData來保存Item_Post操作方法中的數據。

public ActionResult Item_Post() 
    { 
     List<Product> products=new List<Product>() ; 
     int? total=0; 
     HttpCookie cookie= Request.Cookies["myvalue"]; 
     if (Request.Cookies["myvalue"] != null) 
     {   
     some logic here 
     } 
     //save it to TempData for later usage 
     TempData["products"] = products; 

     //return View("Buy", products); 
     //return RedirectToAction("Buy", new {id=products}); 

     return RedirectToAction("Buy"); 
    } 

現在在Buy行動使用TempData,讓您的數據。

[HttpGet] 
public ActionResult Buy() 
{ 
    var products = TempData["products"]; 
    //.. do anything 
} 

希望這會有所幫助。


UPDATE

使用下面的代碼爲Buy行動。

[HttpGet] 
     public ActionResult Buy() 
     { 
      var products = TempData["products"] as List<Product>; 
      return View(products); 
     } 

而現在看來,在產品

@model IEnumerable<Shop_Online.com.Models.Product> 

@foreach (var item in Model) 
{ 
    <div> 
     Item Id: @item.Id 
    </div> 
    <div> 
     Item name: @item.Name 
    </div> 
} 

現在,這應該顯示你所有項目的列表中使用foreach在元素的列表。

或者將TempData分配給模型類的對象,您也可以嘗試使用以下代碼替換上述foreach。

@if (TempData["products"] != null) 
{ 
    foreach (var item in TempData["products"] as List<Product>) 
    { 
     <div> 
      Item Id: @item.Id 
     </div> 
     <div> 
      Item name: @item.Name 
     </div> 
    } 
} 
+0

@用戶你有沒有試過?您的列表包含多少個元素? –

0

是您購買的ActionResult接受List<Product>作爲參數,喜歡的東西:

public ActionResult Buy(List<Product> ids) 
{ 
    ... 
} 

沒有這個它將不知道做什麼用的產品的清單做

+0

,就像你上面提到的一樣。當我使用return View(「購買」,產品)時它工作正常。但問題是url保持不變。我想把url作爲http:// localhost:16916/Home /購買 – User

+0

RedirectToAction會抓取GET方法,你需要POST到/ Home/Buy嗎? – Zac

+0

我想搬到/首頁/購買? – User

1

您可以將產品陣列作爲RedirctToAction的id傳遞。

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction%28v=vs.118%29.aspx

它接受RouteParamter或只值,可以在網址的查詢字符串傳遞。

如果你想使用RedirectToAction,那麼我建議你應該使用TempData。

public ActionResult Item_Post() 
    { 
     List<Product> products=new List<Product>() ; 
     int? total=0; 
     HttpCookie cookie= Request.Cookies["myvalue"]; 
     if (Request.Cookies["myvalue"] != null) 
     {   
     some logic here 
     }     
     TempData["Products"] = products; 
     return RedirectToAction("Buy"); 
    } 

在你買房行動

public ActionResult Buy() 
{ 
    // Get value from TempData 
    var products= (List<Product>)TempData["Products"]; 
}