Reputation: 2305
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.
Upvotes: 1
Views: 412
Reputation: 1039438
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.
Upvotes: 1