output required
result = [Object, Object, Object, Object, Object, Object, Object]
output coming
result = "[{"label":"11-03-2016","value":"159","year":"2000"},{"label":"12-03-2016","value":"3207","year":"2001"},{"label":"13-03-2016","value":"4610","year":"2002"},{"label":"14-03-2016","value":"3418","year":"2003"},{"label":"15-03-2016","value":"551","year":"2004"}]"
What I have tried:
public JsonResult GraphData()
{
GraphModel oGraphModel = null;
List<graphmodel> lstGraphModel = new List<graphmodel>();
int index = 0;
Random random = new Random();
while (index < 5)
{
oGraphModel = new GraphModel();
oGraphModel.label = DateTime.Now.AddDays(index).ToString("dd-MM-yyyy");
oGraphModel.value = random.Next(index, 5000).ToString();
oGraphModel.year = (index + 2000).ToString();
lstGraphModel.Add(oGraphModel);
index++;
}
var jsonSerializer = new JavaScriptSerializer();
string data = jsonSerializer.Serialize(lstGraphModel);
return Json(data, JsonRequestBehavior.AllowGet);
}
<script type="text/javascript">
$(document).ready(function() {
new Morris.Line({
element: 'line-chart',
data: Graph(),
xkey: 'year',
ykeys: ['value'],
async: true,
labels: ['value'],
lineColors: ['#D9534F'],
lineWidth: '2px',
hideHover: true
});
new Morris.Area({
element: 'area-chart',
data: Graph(),
xkey: 'year',
ykeys: ['value'],
async: true,
labels: ['value'],
lineColors: ['#428BCA'],
lineWidth: '2px',
hideHover: true
});
});
function Graph() {
var data = "";
$.ajax({
type: 'GET',
url: '@Url.Action("GraphData", "Home")',
dataType: 'json',
async: true,
contentType: "application/json; charset=utf-8",
data: {},
success: function (result) {
data = result;
},
error: function (xhr, status, error) {
alert(error);
}
});
return data;
}
</script>