所以,我的控制器里面有一个ActionResult,它应该从我的数据库中获取现有的值并将参数返回给视图。
我的方法称为ReadMeasurements,它抓取角度和点数据。如果存在/非空,我想返回角度和点数据。
我注意到我现在设置它的方式,我只返回我的角度数据,因为当角度数据和点数据不为空时,代码只返回角度数据而不是点数据。
如何在方法中返回角度和点数据?
这是我的代码:
[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 (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 = "Points",
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;
}
答案 0 :(得分:5)
创建一个包含它们的类型并返回它:
public class PointAndAngle
{
public Point Point { get; set; }
public Angle Angle { get; set; }
}
var pAndA = new PointAndAngle { Point = p, Angle = a };
return Json(pAndA);
如果你也愿意,可以使用匿名类型:
return Json(new { Point = p, Angle = a});
答案 1 :(得分:2)
尝试类似
的内容public class data
{
public AngleData angle {get; set;}
public PointData point { get; set;}
}
在函数中返回data
类型的对象。