Here's a method inside my controller that reads the values of angle and point data from my database. Then it grabs the data and adds it to a new list and sends the JSON to the view.
I can't simplify this simply putting my if
statements into two ActionResult
s because I can only bind one datasource to one kendo grid.
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult ReadMeasurements([DataSourceRequest] DataSourceRequest request, string viewType)
{
JsonResult json = new JsonResult();
List<AngleData> angledata = UserSession.GetValue(StateNameEnum.Planning, ScreenName.Planning.ToString() + "Angles" + viewType, UserSessionMode.Database) as List<AngleData>;
List<PointData> pointData = UserSession.GetValue(StateNameEnum.Planning, ScreenName.Planning.ToString() + "Points" + viewType, UserSessionMode.Database) as List<PointData>;
if(pointData != null && angledata != null)
{
List<PlanningViewParam> angles = new List<PlanningViewParam>();
foreach (AngleData i in angledata)
{
string col = "#" + ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", (int)(i.color.r * 255), (int)(i.color.g * 255), (int)(i.color.b * 255))).Name.Remove(0, 2);
int angleVal = (int)i.angleValue;
angles.Add(new PlanningViewParam()
{
Color = col,
Label = "Angle",
Value = angleVal,
Number = i.angleNumber
});
}
List<DPlanningViewParam> points = new List<PlanningViewParam>();
foreach (PointData f in pointData)
{
string col = "#" + ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", (int)(f.color.r * 255), (int)(f.color.g * 255), (int)(f.color.b * 255))).Name.Remove(0, 2);
string pointAnglesVal = f.pointAnglesValue;
points.Add(new PlanningViewParam()
{
Color = col,
Label = "Point",
ValueTwo = pointAnglesVal,
Number = f.pointNumber
});
}
return Json(new { Angles = angles, Points = points }, JsonRequestBehavior.AllowGet);
}
if (angledata != null)
{
List<PlanningViewParam> angles = new List<PlanningViewParam>();
foreach (AngleData i in angledata)
{
string col = "#" + ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", (int)(i.color.r * 255), (int)(i.color.g * 255), (int)(i.color.b * 255))).Name.Remove(0, 2);
int angleVal = (int)i.angleValue;
angles.Add(new PlanningViewParam()
{
Color = col,
Label = "Angle",
Value = angleVal,
Number = i.angleNumber
});
}
return json = Json(angles.ToDataSourceResult(request, i => new PlanningViewParam()
{
Color = i.Color,
Label = i.Label,
Value = i.Value,
Number = i.Number
}), JsonRequestBehavior.AllowGet);
}
if (pointData != null)
{
List<PlanningViewParam> points = new List<PlanningViewParam>();
foreach (PointData f in pointData)
{
string col = "#" + ColorTranslator.FromHtml(String.Format("#{0:X2}{1:X2}{2:X2}", (int)(f.color.r * 255), (int)(f.color.g * 255), (int)(f.color.b * 255))).Name.Remove(0, 2);
string pointAnglesVal = f.pointAnglesValue;
points.Add(new PlanningViewParam()
{
Color = col,
Label = "Point",
ValueTwo = pointAnglesVal,
Number = f.pointNumber
});
}
return json = Json(points.ToDataSourceResult(request, f => new PlanningViewParam()
{
Color = f.Color,
Label = f.Label,
Value = f.Value,
Number = f.Number
}), JsonRequestBehavior.AllowGet);
}
return null;
}