Rajveer Singh Rajveer Singh - 2 months ago 14
JSON Question

Return Json Format to View - MVC

I am new to asp.net MVC. I hav to return

Json
from controller to view using
ajax
call, that is perfect.

I want to return below json format to view.

[{
"name": "A",
"data": [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6] },
{
"name": "B",
"data": [3.9, 2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
},
{
"name": "C",
"data": [3.9, 2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
}]


I have tried below modal to return such a format,

public class myModel
{
public List<string> Name { get; set; }
public List<float> Average { get; set; }
}


What should be my model like to return above mentioned format?

Answer

MyModel should be modified like this

public class MyModel
{
    public string name { get; set; }
    public List<float> data { get; set; }
}

as name property is only for a single string, and data is actually the array holding the float values. You can return List<MyModel> or array - MyModel[] as JSON from ajax call

Comments