如何将 cookie 值字符串数组传递给控制器以作为PayPal项循环
本文关键字:控制器 PayPal 循环 cookie 字符串 数组 | 更新日期: 2023-09-27 18:36:04
我正在尝试将我的 CookieCarts 字符串数组(包含购物车项目)传递到我的控制器中,以便循环访问我的PayPal api。
我的观点
var cookiecart = Server.UrlDecode(Request.Cookies["cookieCart"].Value);
@Html.HiddenFor(m => m.CookieCart, new { Value = cookiecart })
Response.Write(cookiecart);
cookiecart:*[{"datetime":"2016-02-25 02:51:49","id":"749","typeid":"13","qty":1,"fullname":"The Matrix","image":"/Content/images/products/online-video.png","price":"69","sku":"MATRIX"}]*
我的模型
public string CookieCart { get; set; }
我的控制器
var cartArray = model.CookieCart;
var cartArray = model.CookieCart;
var itemArray = cartArray.Split(',');
foreach (var t in itemArray)
{item.name = itemArray[0]; }
当我快速观看发送到控制器的数据时,它看起来像这样:
cartArray displays: "[{'"datetime'":'"2016-02-25 02:51:49'",'"id'":'"749'",'"typeid'":'"13'",'"qty'":1,'"fullname'":'"The Matrix'",'"image'":'"/Content/images/products/online- video.png'",'"price'":'"69'",'"sku'":'"MATRIX'"}]"
item.name displays: *"[{'"datetime'":'"2016-02-25 02:51:49'""*
这些都不对。 太令人沮丧了!如何将 Cookie 数组值转换为 C# 数组。
itemArray[0] should be:
itemArray[0][0] = datetime:"2016-02-25 02:51:49",
itemArray[0][1] = id:"749",
itemArray[0][2] = typeid:"13",
itemArray[0][3] = qty:1,
itemArray[0][4] = fullname:"The Matrix",
itemArray[0][5] = image:"/Content/images/products/online-video.png",
itemArray[0][6] = price:"69"
itemArray[0][7] = sku:"MATRIX"
:(
好的,
我想通了。 使用 JSON .Net:
我的控制器
var cookie = Request.Cookies["cookieCart"];
cookieArray = JsonConvert.DeserializeObject<List<CookieCart>>
(Server.UrlDecode(cookie.Value));
我的模型
public class CookieCart
{
public DateTime Datetime { get; set; }
public int Id { get; set; }
public int Typeid { get; set; }
public string Qty { get; set; }
public string Fullname { get; set; }
public string Image { get; set; }
public string Price { get; set; }
public string Sku { get; set; }
}
然后我迭代了PayPal数组项:
foreach (var cartitem in cookiecart)
{
item.name = cartitem.Fullname;
item.currency = "USD";
item.price = cartitem.Price;
item.quantity = cartitem.Qty;
item.sku = cartitem.Sku;
var intPrice = Int32.Parse(cartitem.Price);
subtotal = subtotal + intPrice;
}