我试图遵循
JavaScript和C#中的命名约定.当来回传递JSONized数据时,这会导致一些有趣的问题.当我访问x / y坐标客户端时,我希望该属性为小写,但服务器端为大写.
注意:
public class ComponentDiagramPolygon
{
public List<System.Drawing.Point> Vertices { get; set; }
public ComponentDiagramPolygon()
{
Vertices = new List<System.Drawing.Point>();
}
}
public JsonResult VerticesToJsonPolygon(int componentID)
{
PlanViewComponent planViewComponent = PlanViewServices.GetComponentsForPlanView(componentID, SessionManager.Default.User.UserName, "image/png");
ComponentDiagram componentDiagram = new ComponentDiagram();
componentDiagram.LoadComponent(planViewComponent, Guid.NewGuid());
List<ComponentDiagramPolygon> polygons = new List<ComponentDiagramPolygon>();
if (componentDiagram.ComponentVertices.Any())
{
ComponentDiagramPolygon polygon = new ComponentDiagramPolygon();
componentDiagram.ComponentVertices.ForEach(vertice => polygon.Vertices.Add(vertice));
polygons.Add(polygon);
}
return Json(polygons, JsonRequestBehavior.AllowGet);
}
我理解,如果我能够使用C#属性’JsonProperty’来自定义命名约定.然而,据我所知,这仅适用于我所拥有的课程.
如何在传回客户端时更改System.Drawing.Point的属性?
最佳答案 您可以通过投射到新的匿名类型来作弊:
var projected = polygons.Select(p => new { Vertices = p.Vertices.Select(v => new { x = v.X, y = v.Y }) });
return Json(projected, JsonRequestBehavior.AllowGet);