问题
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