You cannot get the list
or any model
objects passed to RedirectToAction
in your action method. Because a RedirectToAction
causes HTTP 302 (Redirect)
request, which makes the browser to call GET request to the action.
You should use TempData
to preserve the data in Item_Post
action method.
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");
}
And now in the Buy
action use TempData
to get your data.
[HttpGet]
public ActionResult Buy()
{
var products = TempData["products"];
//.. do anything
}
Hope this helps.
UPDATE
use the following code for Buy
action.
[HttpGet]
public ActionResult Buy()
{
var products = TempData["products"] as List<Product>;
return View(products);
}
And now in the view, use foreach
over the list of elements in the 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>
}
Now this should display you the list of all the items.
Or instead of assigning the TempData
to an object of model class you can also try the following code which is the replacement for the above 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>
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…