mercredi 18 avril 2018

.NET update parent and child properties with changes that are in a json string

I am given a json string that ONLY contains the properties that have been modified by the user.

When given this string, I need to pull the original object and update it with the changes that are in the json string.

Sample class

public class ParentObject
{
    public int ID { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public List<ChildObject> Children { get; set; }
}

public class ChildObject
{
    public int ID { get; set; }
    public string PropA { get; set; }
    public string PropB { get; set; }
}

I have an asp.net grid (master / detail) that lets the user edit both the parent object and the children.
The changes are submitted to the server as a JSon string containing ONLY the ID and the field that changed.
Ex. [{"ID":"0","Prop1":"Test1",}]
I must then "update" the "original" object with the properties that changed.

I have tried JsonConvert to convert the Json to a dynamic object and AutoMapper to "merge" the dynamic object with the original but that did not work at all.

Any ideas?

I could use reflection to iterate through the properties of the "original" object and then compare those with the Json string BUT I was wonder if there is some other library like AutoMapper that can do this easier?





Aucun commentaire:

Enregistrer un commentaire