我有以下课程:
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类如下:
public class LookUp
{
public int Id {get;set;}
public string Name { get; set; }
public int InsertUserId {get; set;}
public DateTime InsertDate {get; set;}
}
然后我为这些类提供了ViewModel,如:
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; }
}
现在在我的控制器中,我正在做这样的事情:
public IHttpActionResult Get(int adId)
{
var ad = AdService.Get(adId);//Getting ad from DB
AdModel adModel = new AdModel();
adModel.InjectFrom(ad);
return Ok(adModel);
}
我的问题是ValueInjecter
仅复制广告的第一级属性,例如Id, Title, UrlTitle
,但它没有将LookUp
复制到LookUpModel
属性中。
答案 0 :(得分:0)
@Usman Khalid::您可以使用newtonsoft JSON库对对象进行序列化和反序列化。 然后在反序列化对象上使用injectfrom方法可以解决您的问题。
example: Admodel model = new Admodel();
var s = JsonConvert.SerializeObject(model);
UserModel user = JsonConvert.DeserializeObject<UserModel>(s);
user.InjectFrom(user);