valueinjecter not copying nested properties values

允我心安 提交于 2020-01-25 00:25:28

问题


I have following class:

public class Ad
{
    public int Id {get;set;}

    public string Title { get; set; }

    public string UrlTitle { get; set; }

    public LookUp Color {get;set}

    public LookUp Condition {get;set}

    public int InsertUserId {get; set;}

    public DateTime InsertDate {get; set;}
}

LookUp class is as follows:

public class LookUp
{
     public int Id {get;set;}

     public string Name { get; set; } 

     public int InsertUserId {get; set;}

     public DateTime InsertDate {get; set;}
}

Then I have ViewModels for these classes like:

public class AdModel
{
    public int Id {get;set;}

    public string Title { get; set; }

    public string UrlTitle { get; set; }

    public LookUpModel Color {get;set}

    public LookUpModel Condition {get;set}
}

public class LookUpModel
{
     public int Id {get;set;}

     public string Name { get; set; } 
}

Now in my controller, I am doing something like this:

public IHttpActionResult Get(int adId)
{
    var ad = AdService.Get(adId);//Getting ad from DB

    AdModel adModel = new AdModel();
    adModel.InjectFrom(ad);
    return Ok(adModel);
}

My problem is ValueInjecter is only copying the first level properties of the Ad like Id, Title, UrlTitle but it is not copying the LookUp into LookUpModel property.


回答1:


@Usman Khalid: you can use serialize and deserialize objects using newtonsoft JSON library. and then used injectfrom method on the deserializing object your problem maybe resolve.

example: Admodel model = new Admodel();
 var s = JsonConvert.SerializeObject(model);
 UserModel user = JsonConvert.DeserializeObject<UserModel>(s);
user.InjectFrom(user);


来源:https://stackoverflow.com/questions/42766504/valueinjecter-not-copying-nested-properties-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!