Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Right Now i am hard coding data into JQuery i want data to be passed via C#

What I have tried:

controller

public ActionResult GetPiechartData()
{
return View();
}


View
<html>
<head>
<script>
$(document).ready(function () {
var m1 = new Morris.Line({
element: 'line-chart',
data: [
{ y: '2006', a: 30, b: 20 },
{ y: '2007', a: 75, b: 65 },
{ y: '2008', a: 50, b: 40 },
{ y: '2009', a: 75, b: 65 },
{ y: '2010', a: 50, b: 40 },
{ y: '2011', a: 75, b: 65 },
{ y: '2012', a: 100, b: 90 }
],
xkey: 'y',
ykeys: ['a', 'b'],
labels: ['Series A', 'Series B'],
lineColors: ['#D9534F', '#428BCA'],
lineWidth: '2px',
hideHover: true
});
});
</script>
</head>
<body>
< div id="line-chart" class="body-chart">
</div>
</body>
</html>
Posted
Updated 10-Mar-16 21:02pm
v4
Comments
Karthik_Mahalingam 10-Mar-16 12:29pm    
use ajax to get the data from server
MayankSemwal 11-Mar-16 0:41am    
can you tell me how.
Karthik_Mahalingam 11-Mar-16 1:02am    
check this link to fetch the data using ajax
http://www.tutorialspoint.com/jquery/ajax-jquery-ajax.htm

1 solution

Try like this

Javascript

Get the data from controller using ajax
jQuery ajax() Method[^]
jQuery.ajax() | jQuery API Documentation[^]

HTML
<script>
    $(document).ready(function () {

        var link = '@Url.Action("GetData", "Home")';

        $.ajax({
            type: "POST",
            url: link,
            //data: args,
            dataType: "json",
            success: function (data) {
                genChart(data);
            }
        }); 
    });

    function genChart(data) {
        var m1 = new Morris.Line({
            element: 'line-chart',
            data: data,
            xkey: 'y',
            ykeys: ['a', 'b'],
            labels: ['Series A', 'Series B'],
            lineColors: ['#D9534F', '#428BCA'],
            lineWidth: '2px',
            hideHover: true
        });
    }
</script>


Controller
Get the data from a List and convert it to JSON format and return it to the Ajax method

C#
public ActionResult GetData()
        {
            var data = new[] {
                new { y = "2006", a = 30, b = 20 },
                new { y = "2007", a = 75, b = 65 },
                new { y = "2008", a = 50, b = 40 },
                new { y = "2009", a = 75, b = 60 },
                new { y = "2010", a = 50, b = 80 },
                new { y = "2011", a = 30, b = 50 }
            };  // This is  hardcoded data, you can get the data from a List 
            return Json(data);
            
        }


Let me know if you face any issue.
 
Share this answer
 
Comments
MayankSemwal 11-Mar-16 3:03am    
Thanks for your time Karthik, i am able to get results from your code, but i am trying this way and not able to display results please have a look

Model
<pre>
public class GraphModel
{
public string label { get; set; }
public string value { get; set; }
public string year { get; set; }
}
</pre>
Controller
<pre>
public ActionResult 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);
}
</pre>
VIEW
<pre>
<script>
$(document).ready(function () {
var link = '@Url.Action("GraphData", "Home")';
$.ajax({
type: "POST",
url: link,
//data: args,
dataType: "json",
success: function (data) {
genChart(data);
}
});
});

function genChart(data) {
var m1 = new Morris.Line({
element: 'line-chart',
data: data,
xkey: 'year',
ykeys: ['value'],
labels: ['value'],
lineColors: ['#D9534F'],
lineWidth: '2px',
hideHover: true
});
}
</script>
</pre>
Karthik_Mahalingam 11-Mar-16 3:23am    
use this code in the controller

public ActionResult 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++;
}

return Json(lstGraphModel);
}
Karthik_Mahalingam 11-Mar-16 3:27am    
let me know if the above code works fine
MayankSemwal 11-Mar-16 3:29am    
Yes this code works fine. Thanks man You are a Savior.
Karthik_Mahalingam 11-Mar-16 3:37am    
welcome mayank.
if it helps, Please close this post, by marking it as answer.
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900