Avinash
Avinash

Reputation: 143

jqPlot Chart using asp.net MVC, JSON

I am new to MVC and jQuery and jqPlot.

Can anyone give example where I can find demo projects or step by step procedure to achive.

What I need is:

Thanks a lot in advance.

===============================================

Tried with below code, it is always going to ERROR part, can anyone explain what is the reason?

<!--[if lt IE 9]><script language="javascript" type="text/javascript" src="../Scripts/excanvas.js"></script><![endif]-->
        <script src="../js/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script type="text/javascript" language="javascript" >
            $(document).ready(function () {
                $("#btnClick").click(function () {
                    alert('Inside Click Function');
                    $.ajax(
                        type: 'GET',
                        url: "JSONSample.aspx/GetItSerialized",
                        processData: true,
                        data: {},
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            $.each(data.d, function (index, item) {
                                alert('Inside Success');
                            });
                            //alert('Inside Success');
                            //processData(data);
                        },
                        error: function(jqXHR, textStatus, errorThrown) {alert('Error');}

                    });

                    function processData(data) {
                        alert('test');
                    }
                });
            });
        </script>


    <body>
        <form id="form1" runat="server">
        <div>  
        <input id="btnClick" runat="server" value="Click here" type="button" />
        </div>
        </form>
    </body>

public string GetItSerialized()
        {
            Employee oEmployee1 = new Employee { Name = "ABC", ID = "111", Age = "30" };
            Employee oEmployee2 = new Employee { Name = "FDF", ID = "rtrt", Age = "31" };
            Employee oEmployee3 = new Employee { Name = "dfdf", ID = "fdfd", Age = "20" };

            List<Employee> listEmp = new List<Employee>
            {
                oEmployee1,
                oEmployee2,
                oEmployee3
            };

            string str = (new JavaScriptSerializer().Serialize(listEmp));

            return str;
        }

    }

    public class Employee
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string ID { get; set; }
    }

Upvotes: 0

Views: 5595

Answers (1)

lucask
lucask

Reputation: 2290

Well, first of all code you provided is an ASP.NET WebForms sample.
Tutorials on how to use jqPlot with MVC: here or here.
To answer rest of your questions:

  1. How to return JSON to a View?
    See answer no. 4

  2. While adding View - it should be partial view or normal view?
    It really depends on what you are trying to accomplish. If you want to create something like a widget then use partial views.

  3. How to get JSON from view using jQuery?
    I'm not really sure what do you mean.

  4. How to pass JSON object to jqPlot?
    Call controller action that will return json object. Follow this Example with this tutorial

Upvotes: 4

Related Questions