在MVC4中使用RedirectToAction发送列表 [英] Sending a list using RedirectToAction in 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");
}
现在,当我仅使用return View("Buy",products)时,我将获得输出,并且Url仍然与我要更改的Url相同,并且在使用时
Now when I use only return View("Buy", products) I am getting the output and the Url remains same as I want to change the Url and when I use
return RedirectToAction("Buy", products);
我在将表格发布到购买"时遇到错误.在RedirectToAction内传递的参数是否适当,或者是否需要其他条件. 这是动作
I am getting error as I want to post the form to Buy. Are the parameters passed within the RedirectToAction appropriate or does it require anything else. Here is the Action
@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>Rs.@ViewBag.total</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,现在应该如何在视图中替换下面的代码
And now how should I replace the below code within my View If I use TempData
@foreach(var item in Model)
{
@Html.DisplayFor(model=>model.Name
/some more code/
}
推荐答案
您无法在操作方法中获取传递给RedirectToAction
的list
或任何model
对象.因为RedirectToAction
会导致HTTP 302 (Redirect)
请求,这会使浏览器调用该操作的 GET 请求.
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.
您应该使用TempData
将数据保存在Item_Post
操作方法中.
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");
}
现在在Buy
动作中,使用TempData
来获取数据.
And now in the Buy
action use TempData
to get your data.
[HttpGet]
public ActionResult Buy()
{
var products = TempData["products"];
//.. do anything
}
希望这会有所帮助.
更新
将以下代码用于Buy
操作.
[HttpGet]
public ActionResult Buy()
{
var products = TempData["products"] as List<Product>;
return View(products);
}
现在在视图中,在产品中的元素列表上使用foreach
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.
或者代替将TempData
分配给模型类的对象,您还可以尝试以下代码,代替上面的foreach.
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>
}
}
这篇关于在MVC4中使用RedirectToAction发送列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!