NeilSah
NeilSah

Reputation: 31

In MVC I want to create ViewData with JSON from controller and use it in view for google chart

In below way in controller I am creating list and converting it into JSON serialization format

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);

Now, I want to use this created ViewData into View

Upvotes: 0

Views: 1209

Answers (1)

Shyju
Shyju

Reputation: 218962

If you want to read and store it to a javascript variable, you can do this

var itemArray = @Html.Raw(ViewData["JsonCoverageCounts"]);

//Let's print it in console
console.log(itemArray);

itemArray will be an array of objects, each with a Count and Description property

Upvotes: 1

Related Questions