Reputation: 1687
I have spent the last couple of days trying to read up and test a variety of methods for taking a property(column) in an entity within entity framework and retrieving an average from it that I then pass to JSON. I have gotten close a few times but always failed. Finally when I have the JSON I want to return it in an indented format to use in a JQuery visualization library.
My Controller is here:
public virtual JsonResult GetData()
{
var result = new FactSurveryResultsQueries().GetAverages();
return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = result };
}
Here is my View Model:
namespace Provider.Models
{
public class FactSurveyResultsViewModel //: BaseGraphViewModel
{
public FactSurveyResultsViewModel() { }
//public List<KPIUserControl> customerSatisfactionChart = new List<KPIUserControl>();
////public List<string> customerSatisfactionChart = new List<string>();
//public Dictionary<string, decimal> a { get; set; }
public double? EaseRequestingHelp { get; set; }
public double? TimeReachQualifiedTech { get; set; }
public double? TimeProvideSolution { get; set; }
public double? CompletenessOfSolution { get; set; }
public double? TechAbilityProdKnowledge { get; set; }
public double? HowWellKeptInformed { get; set; }
public double? OverallServiceOpinion { get; set; }
}
}
Finally this is my current iteration to retrieve just one property and average it although this is currently null.
namespace Provider.Queries
{
public class FactSurveryResultsQueries
{
public FactSurveyResultsViewModel GetAverages()
{
var result = new FactSurveyResultsViewModel();
using (var db = new ATSNavigatorDBEntities())
{
var thing =
from p in db.Fact_SurveyResults
group p by 1 into a
select new FactSurveyResultsViewModel { EaseRequestingHelp = a.Average(p.EaseRequestingHelp)};
}
return result;
}
}
}
Can anybody point me towards retrieving one property and having it accessible from my controller as indented JSON so I can consume it and then I can replicate this for other properties? Thanks.
Upvotes: 0
Views: 226
Reputation: 3987
You can use Json.NET to serialize .NET objects into JSON. It has a formatting option that produces indented JSON.
You can install Json.NET via NuGet from within Visual Studio. Once you have installed it, you can serialize via JsonConvert.SerializeObject()
method.
Upvotes: 1