I have a view in ASP.NET MVC3 that makes a call to an AsyncController to get a partial view. I also want to set some data as a JSON string to a cookie that I can read from the jQuery.
Here is my View:
$.ajax({
type: "POST",
url: '/ProductAsync/GetProductData',
data: JSON.stringify({
Product: thisProduct
}),
contentType: "application/json; charset=utf-8",
success: function (returndata) {
$('div#' + thisProduct).empty().html(returndata);
var productDataJson = $.cookie(thisProduct + "-ProductData");
alert(productDataJson);
}
});
Here is my AsyncControlleraction:
public class ProductAsyncController : AsyncController
{
[HttpPost]
public ActionResult GetProductData(string Product)
{
ProductData model = ProductModel.GetProductData(Product);
Response.Cookies.Add(new HttpCookie(Product+"-ProductData", (new JavaScriptSerializer()).Serialize(model)));
return PartialView("~/Views/Product/_ProductData.cshtml", model);
}
}
I know that the Model is returned to the View as expected. The HTML from the partial shows up just fine. But it seems the cookie is not getting set by:
Response.Cookies.Add(new HttpCookie(Product+"-ProductData", (new JavaScriptSerializer()).Serialize(model)));
I cannot see the cookie in my browser's cookie storage location and the alert() shows null. I know that the JavaScriptSerializer is working: I tested by putting the string in ViewData and displaying it on the page.
What am I doing wrong with respect to setting the cookie?