如何将cookie值字符串数组传递给控制器​​以作为PayPal项

时间:2016-02-25 06:04:26

标签: c# arrays cookies paypal

我试图将我的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"

:(

1 个答案:

答案 0 :(得分:0)

好吧,我明白了。使用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;
        }