2016-09-23 34 views
0

的列表我有一個簡單對象如下:C# - 移調對象插入值

public partial class RepackViewIndex 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.None)] 
    public int ID { get; set; } 

    public DateTime? DateRequested { get; set; } 

    public string OrderNo { get; set; } 

    [StringLength(30)] 
    public string Customer { get; set; } 

    [StringLength(30)] 
    public string RepackFrom { get; set; } 

    [StringLength(30)] 
    public string RepackTo { get; set; } 

    public int? Qty { get; set; } 

    public int? ReworkTime_c { get; set; } 

    [StringLength(8)] 
    public string TTLTime { get; set; } 

    public int? QtyCompleted { get; set; } 

    public DateTime? StartDate { get; set; } 

    public DateTime? PlannedCompletion { get; set; } 

    [StringLength(50)] 
    public string RequestedBy { get; set; } 

    [StringLength(30)] 
    public string EnteredBy { get; set; } 

    [StringLength(1)] 
    public string Status { get; set; } 

    [StringLength(50)] 
    public string Priority { get; set; } 
} 

然而,我想顯示此對象在前端的表。 (前端在Angular2中)。我認爲最好的辦法是將對象轉換成A字典或類似的,但我不知道最好的方法或如何實現,所以任何幫助將不勝感激。

或者,如果有任何angular2大師有使用數據的方式,因爲它是在表中顯示我全部耳朵。我正在使用PrimeNG來處理我的組件。

非常感謝,

+0

序列化到JSON使用'for..in'在JavaScript中迭代鍵,然後可以根據需要在Angular中構建出您的表。 – Lloyd

回答

1

如果您只是想你的價值觀,通過屬性名字典,你可以添加一個實例方法和使用反射:

public Dictionary<string, object> ToDictionary() 
{ 
    var dict = new Dictionary<string, object>(); 
    foreach (var prop in GetType().GetProperties()) 
    { 
     dict.Add(prop.Name, prop.GetValue(this)); 
    } 

    return dict; 
}