valueinjecter不复制嵌套的属性值 [英] valueinjecter not copying nested properties values

查看:95
本文介绍了valueinjecter不复制嵌套的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆