Skip to main content
1 of 2
cha
  • 315
  • 1
  • 5

First of all, get rid of this line:

JsonResult json = new JsonResult();

Just use return new JSON(...).

Now, you have repeating chunks of codes when you construct the angles and points Lists. I recommend you extract them to separate methods. If you do not want to clutter your code with the methods use the delegate() or Func<>() to create a functions inside your method.

So, with that your code will be simpler:

    public ActionResult ReadMeasurements([DataSourceRequest] DataSourceRequest request, string viewType)
    {
        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 = BuildAngles(angledata);
            List<DPlanningViewParam> points = BuildPoints(pointData);
            return new Json(new { Angles =  angles, Points = points }, JsonRequestBehavior.AllowGet);
        }
        else if (angledata != null)
        {
            List<PlanningViewParam> angles = BuildAngles(angledata);
            return new Json(angles.ToDataSourceResult(request, i => new PlanningViewParam()
            {
                Color = i.Color,
                Label = i.Label,
                Value = i.Value,
                Number = i.Number
            }), JsonRequestBehavior.AllowGet);

        }
        else if (pointData != null)
        {
            List<DPlanningViewParam> points = BuildPoints(pointData);
            return new Json(points.ToDataSourceResult(request, f => new PlanningViewParam()
            {
                Color = f.Color,
                Label = f.Label,
                Value = f.Value,
                Number = f.Number
            }), JsonRequestBehavior.AllowGet);

        }

        return null;

    }

BTW, I noticed that the JSON for the first condition pointData != null && angledata != null is returned differently. You return just new { Angles = angles, Points = points } allowing the .Net engine to serialise the data for your. For other conditions you explicitly list all elements. You either did not test the first condition, or the explicitly listing all elements is not required, as the engine does the job for just fine. If the latter is the case then use return new Json(new { Angles = angles }, JsonRequestBehavior.AllowGet); instead of angles.ToDataSourceResult(request, i... Try this and see how it goes:

    public ActionResult ReadMeasurements([DataSourceRequest] DataSourceRequest request, string viewType)
    {
        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 = BuildAngles(angledata);
            List<DPlanningViewParam> points = BuildPoints(pointData);
            return new Json(new { Angles =  angles, Points = points }, JsonRequestBehavior.AllowGet);
        }
        else if (angledata != null)
        {
            List<PlanningViewParam> angles = BuildAngles(angledata);
            return new Json(new { Angles =  angles }, JsonRequestBehavior.AllowGet);

        }
        else if (pointData != null)
        {
            List<DPlanningViewParam> points = BuildPoints(pointData);
            return new Json(new { Points = points }, JsonRequestBehavior.AllowGet);

        }

        return null;

    }
cha
  • 315
  • 1
  • 5