---
title: "How to pass json data from controller to view"  
description: "How to pass json data from controller to view"  
author: "Manish Kumar"  
published: 2017-06-14  
updated: 2017-06-14  
canonical: https://www.mindstick.com/forum/34308/how-to-pass-json-data-from-controller-to-view  
category: "c#"  
tags: ["c#", "json", "mvc"]  
reading_time: 2 minutes  

---

# How to pass json data from controller to view

**[Partial view](https://www.mindstick.com/articles/1132/auto-refresh-partial-view-in-asp-dot-net-mvc)**

```
var dataa;    $.ajax({        url: ServerUrl + '/Dashboard/GetData',        type: 'POST',        cache: false,        dataType: 'text',        async: true,        error: function (xhr) {            //alert('Error: ' + xhr.statusText);        },        success: function (result) {            debugger;            dataa = result;            var chart = c3.generate({                data: {                    type: 'bar',                    json: [                        dataa                    ],                    keys: {                        x: 'indicator',                        value: ['total']                    }                },                axis: {                    x: {                        type: 'category'                    }                },                bar: {                    width: {                        ratio: 0.5                    }                }            });        }    });
```

[Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) Json code

```
public string GetData(){return "{ 'indicator': 'A', 'total': 10 },{ 'indicator': 'B', 'total': 20 },{ 'indicator': 'C', 'total': 30 }";} 
```

can any one solve my [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) ...my problem is When i use the above code it doesn't work but if I pass [json data](https://www.mindstick.com/forum/782/how-to-send-request-amp-amp-response-from-json-data-using-asp-dot-net) as specified in this JS Fiddle [link](https://www.mindstick.com/articles/23633/link-builder), it works. Am I passing the JSON data incorrectly from controller.?

Please help.

Thanks in [Advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)

## Replies

### Reply by Sunil Singh

You are not returning [JSON](https://www.mindstick.com/forum/34446/convert-json-string-to-object) from method GetData. Do this way to return JSON

```
public JsonResult
GetData()       
{            var
data = new[] {         
new { indicator= "X", total = 100 },         
new { indicator= "Y", total = 200 },         
new { indicator= "Z", total = 300 }      
};             return Json(data, JsonRequestBehavior.AllowGet);        }
```

Ajax calling like this

```
$.ajax({         
cache: false,         
type: "GET",         
url: URL,         
dataType: 'json',         
success: function (result) {              console.log(result);         
},         
error: function (xhr, ajaxOptions,
thrownError) {              alert('Failed to retrieve data.');         
}      });
```

Hope this helps you..


---

Original Source: https://www.mindstick.com/forum/34308/how-to-pass-json-data-from-controller-to-view

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
