【问题标题】:Sending a list using RedirectToAction in MVC4在 MVC4 中使用 RedirectToAction 发送列表
【发布时间】:2014-12-29 06:30:39
【问题描述】:

我有以下代码

        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 以及何时使用

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>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,我应该如何在我的视图中替换以下代码

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

【问题讨论】:

  • 如果有什么不清楚的地方请评论
  • @ssilas777 它不起作用,因为我正在传递值列表
  • 你能发布你的动作吗Buy.
  • @karthik 请检查代码

标签: asp.net-mvc-4


【解决方案1】:

您无法在操作方法中将list 或任何model 对象传递给RedirectToAction。因为RedirectToAction引起HTTP 302 (Redirect)请求,这使得浏览器调用GET请求到action。

您应该使用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
}

希望这会有所帮助。


更新

将以下代码用于Buy 操作。

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

现在在视图中,使用 foreach 覆盖产品中的元素列表

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

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

现在这应该会显示所有项目的列表。

或者,除了将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>
    }
}

【讨论】:

  • @User 你试过了吗?您的列表包含多少个元素?
  • 临时数据不是要走的路。
  • @KolobCanyon,什么是更好的选择,为什么不是 TempData?
【解决方案2】:

您可以将产品数组作为 ID 从 RedirctToAction 传递。

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

它接受 RouteParamter 或只是您在 url 的查询字符串中传递的值。

如果你想使用 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"];
}

【讨论】:

    【解决方案3】:

    您的 Buy ActionResult 是否接受 List&lt;Product&gt; 作为参数,例如:

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

    没有它,它将不知道如何处理产品列表

    【讨论】:

    • 和你上面说的完全一样。当我使用 return View("Buy", products) 时效果很好。但问题是网址保持不变。我希望网址为localhost:16916/Home/Buy
    • RedirectToAction 会抓取 GET 方法,是否需要 POST 到 /Home/Buy?
    • 我想搬到/Home/Buy?
    猜你喜欢
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2016-08-25
    • 2012-04-03
    相关资源
    最近更新 更多