【发布时间】:2017-08-03 15:19:38
【问题描述】:
我有以下课程:
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 属性中。
【问题讨论】:
-
好像 AdModel 类没有“InjectFrom”方法...
-
我用的是valueinjecter,InjectFrom方法是ValueInjecter提供的。
-
使用
Mapper.Map而不是.InjectFrom在此处阅读:github.com/omuleanu/ValueInjecter;在映射之前,您需要为每对类型调用Mapper.AddMap(并在 AddMap 中调用 Mapper.Map) -
甚至 Mapper.Map 都不起作用,但我在这里找到了一种解决方案 qubanshi.cc/questions/950728/…,它起作用了。谢谢
标签: c# asp.net .net mapping valueinjecter