如何将结果绑定到C#中的构造函数属性?

时间:2018-01-11 15:19:03

标签: c# json

我已经构建了一个包含属性的接口:

 public interface IDatasource
    {
        string Name { get; }
        string Description { get; }
        List<GraphPoint> Point { get; set; }
    }

然后我创建了一个实现此接口的类:

public class RiskDS : IDatasource
{
    public string Name { get; set; }
    public string Description { get; set; }
    public List<GraphPoint> Point { get; set; }

    public RiskDS()
    {
        Name = "Risk overview";
        Description = "Blaat";
        Point = new List<GraphPoint> { new GraphPoint()
        {   Name = "Result of GetNetRisk",
            Description = "Result of GetNetRisk",
            Xname = "Result of GetNetRisk",
            Yname = "Result of GetNetRisk",
            Xaxis = new List<int> { 2, 6, 7, 8, 9, 1, 4, 1 }, //Result of GetNetRisk
            Yaxis = new List<int> { 2, 6, 7, 8, 9, 1, 4, 1 }  //Result of GetNetRisk
        }
        };
    }


    public static string GetNetRisks(long organizationId)
    {
        var _riskDashboardBusiness = new RiskDashboard();           
        var result = _riskDashboardBusiness.GetScoresOfLastTwoPeriods(organizationId, Data.Common.Enums.ORM.RiskScoringType.Net);
        return JsonConvert.SerializeObject(result, Formatting.None);
    }
}

到目前为止一直很好,但现在我想将GetNetRisks的结果绑定到构造函数中的属性。之后,我想将此对象提供给控制器,以便我可以使用angularjs将其绑定在前端:

这是我的controller功能:

[HttpGet]
        public string GetDataSource(string id)
        {
           // var type = (DataSourceType)id;
            DataSourceType type;
            Enum.TryParse(id, out type);
            var o = _dashboarBusiness.GetDataSource(type = DataSourceType.CONTROL);
            return Newtonsoft.Json.JsonConvert.SerializeObject(o);
    }

在我之前的问题中,您可以找到有关我的工厂模式实施的更多信息:

How can I return the values from the interface methods into a JSON object?

如何将结果绑定到构造函数中,以便将其提供给控制器?

亲切的问候

1 个答案:

答案 0 :(得分:0)

您是否在询问Json的简单反序列化?

public RiskDS()
{
    Name = "Risk overview";
    Description = "Blaat";
    var netRisks = GetNetRisks(/* organization id */);
    Point = Newtonsoft.Json.JsonConvert.DeserializeObject<List<GraphPoint>>(netRisks);
}