valueinjecter不复制嵌套的属性值 [英] valueinjecter not copying nested properties values
问题描述
我有以下课程:
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类如下:
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;}
}
然后我为这些类提供了ViewModel:
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);
}
我的问题是ValueInjecter
只是像Id, Title, UrlTitle
这样复制广告的第一级属性,而没有将LookUp
复制到LookUpModel
属性.
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.
推荐答案
@Usman Khalid:,您可以使用newtonsoft JSON库对对象进行序列化和反序列化. 然后在反序列化对象上使用injectfrom方法可以解决您的问题.
@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);
这篇关于valueinjecter不复制嵌套的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!