Reputation: 537
I am having the following code
var s = Request.Cookies["myvalue"].Value;
string[] values = s.Split(',').Select(x => x.Trim()).ToArray();
Here is the Jquery
$(document).ready(function () {
$("#btnAddToCart").click(function () {
$(this).css("background-color", "green");
if (typeof $.cookie('myvalue') == 'undefined') {
$.cookie("myvalue", 0, { expiry: 1 });
}
var a = $(".img1").attr("id");
var imgsrc = $.cookie("myvalue").split(",");
var count = 0;
var totalitems = 0;
for (var i = 0; i < imgsrc.length; i++) {
if (a == imgsrc[i]) {
break;
}
else {
count++;
}
}
var lenghtOfArry = imgsrc.length;
if (count >= lenghtOfArry) {
$.cookie("myvalue", $.cookie("myvalue") + "," + a, { expiry: 10 });
}
else {
alert('This value already exist');
}
for (var i = 0; i < imgsrc.length; i++) {
totalitems++;
}
$(".para").text("CART" + " " + "(" + totalitems + ")");
$(".para").css("color", "#fff");
$(".para").css("text-align", "center");
});
});
When I try to access the cookie value while debugging its giving the value as
0%2C40%2C50%2C60%2C80%2C9
I want the values to be as
0 4 5 6 8 9
How to do it?
Upvotes: 2
Views: 3940
Reputation: 4481
Your cookie value seems to be url encoded. Try the following:
var s = Request.Cookies["myvalue"].Value;
s = HttpUtility.UrlDecode(s ?? string.Empty);
string[] values = s.Split(',').Select(x => x.Trim()).ToArray();
Upvotes: 1