在控制器的下面,我创建列表并将其转换为JSON序列化格式
List<Models.EmployeeCounts> coverageOffersCount = new List<Models.EmployeeCounts>();
coverageOffersCount.Add(new EmployeeCounts() { Description = "Not Offered", Count = 10});
coverageOffersCount.Add(new EmployeeCounts() { Description = "Waived", Count = 20});
coverageOffersCount.Add(new EmployeeCounts() { Description = "Enrolled", Count = 30});
JavaScriptSerializer serializer = new JavaScriptSerializer();
ViewData["JsonCoverageCounts"] = serializer.Serialize(coverageOffersCount);
现在,我想将这个创建的ViewData用于View
答案 0 :(得分:1)
如果您想要将其读取并存储到javascript变量,则可以执行此操作
var itemArray = @Html.Raw(ViewData["JsonCoverageCounts"]);
//Let's print it in console
console.log(itemArray);
itemArray
将是一个对象数组,每个对象都有Count
和Description
属性