1

I am trying to get data from the following class:

 public class SalesData
    {
        public string RepName { get; set; }

        public string DateString { get; set; }

        public decimal TotalSales { get; set; }

        public decimal RepSales { get; set; }
    }

    public static class SalesDataBuilder
    {
        public static List<SalesData> GetCollection()
        {
            return new List<SalesData>
            {
                new SalesData
                {
                    RepName = "Nancy Davolio",
                    DateString = "Aug 2010",
                    TotalSales = 10458,
                    RepSales = 2015
                },

                new SalesData
                {
                    RepName = "Nancy Davolio",
                    DateString = "Sept 2010",
                    TotalSales = 21598,
                    RepSales = 6003
                },  };
        }

Using Json in my controller, I tried the following:

   public JsonResult IndexJson()
        {
            IEnumerable<SalesData> person = (from e in SalesData

                                                    select new SalesData
                                                {
                                                    RepName = e.RepName,
                                                    RepSales = e.RepSales 
                                                });
            return Json(person);
        }

Certainly I am doing something wrong, being very new to json. I would appreciate your suggestions. Thanks in advance.

6
  • 3
    so... what is your question/issue? Commented Apr 8, 2012 at 4:33
  • The jsonResult in the controller does not work! Commented Apr 8, 2012 at 4:56
  • 2
    How doesn't it work? Can you not consume this on the client side? Is there an error or compilation problem? There is no question here. Commented Apr 8, 2012 at 5:44
  • Forgive the question, but does this code even build? Should it read "(from e in GetCollection()"? Commented Apr 8, 2012 at 5:53
  • The code does not build. I used return Json(SalesDataBuilder.GetCollection()); it works in Ajax. What I am trying to do is to use json as data source for Kendo grid. Commented Apr 8, 2012 at 6:00

1 Answer 1

1

Your code doesn't even compile. You never call the SalesDataBuilder.GetCollection method. Try like this:

public ActionResult IndexJson()
{
    IEnumerable<SalesData> person = 
        from e in SalesDataBuilder.GetCollection()
        select new SalesData
        {
            RepName = e.RepName,
            RepSales = e.RepSales
        };
    return Json(person, JsonRequestBehavior.AllowGet);
}

Notice that I am passing JsonRequestBehavior.AllowGet to the Json method which is necessary if you are invoking this action using the GET verb.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.