0

我在使用 AutoMapper 将域模型映射到视图模型时遇到了一点困难。

我的控制器代码是:

    //
    // GET: /Objective/Analyst
    public ActionResult Analyst(int id)
    {
        var ovm = new ObjectiveVM();
        ovm.DatePeriod = new DateTime(2013, 8,1);
        var objectives = db.Objectives.Include(o => o.Analyst).Where(x => x.AnalystId == id).ToList();
        ovm.ObList = Mapper.Map<IList<Objective>, IList<ObjectiveVM>>(objectives);
        return View(ovm);
    }

我收到一个错误ovm.ObList = Mapper....(ObList 以红色下划线显示错误):

'ObList': cannot reference a type through an expression; try 'Objectives.ViewModels.ObjectiveVM.ObList' instead

我的目标课程是:

   public class Objective
   {
    public int ObjectiveId { get; set; }
    public int AnalystId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public Analyst Analyst { get; set; }
   }

我的 ObjectiveVM(视图模型)是:

   public class ObjectiveVM
   {
    public DateTime DatePeriod { get; set; }
    public class ObList
       {
        public int ObjectiveId { get; set; }
        public int AnalystId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string AnalystName { get; set; }
        public bool Include { get; set; }
       }
   }

在我的 startup/global.asax.cs 中,我使用 AutoMapper 将 Objective 映射到 ObjectiveVM:

   Mapper.CreateMap<Objective, ObjectiveVM.ObList>()
    .ForMember(dest => dest.Include, opt => opt.Ignore())
    .ForMember(dest => dest.AnalystName, opt => opt.MapFrom(y => (y.Analyst.AnalystName)));

任何帮助将非常感激,

标记

4

2 回答 2

0

好的,感谢所有建议 - 我最终得到的是:

控制器:

    //
    // GET: /Objective/Analyst

    public ActionResult Analyst(int id)
    {
        var ovm = new ObjectiveVM().obList;
        var objectives = db.Objectives.Include(o => o.Analyst).Where(x => x.AnalystId == id).ToList();
        ovm = Mapper.Map<IList<Objective>, IList<ObjectiveVM.ObList>>(objectives);
        var ovm2 = new ObjectiveVM();
        ovm2.obList = ovm;
        ovm2.DatePeriod = new DateTime(2013, 8,1);
        return View(ovm2);
    }

视图模型:

public class ObjectiveVM
{
    public DateTime DatePeriod { get; set; }
    public IList<ObList> obList { get; set; }
    public class ObList
    {
        public int ObjectiveId { get; set; }
        public int AnalystId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string AnalystName { get; set; }
        public bool Include { get; set; }
    }
}

创建地图:

 Mapper.CreateMap<Objective, ObjectiveVM.ObList>()
            .ForMember(dest => dest.Include, opt => opt.Ignore())
            .ForMember(dest => dest.AnalystName, opt => opt.MapFrom(y => (y.Analyst.AnalystName)))
            ;

如果我误解了任何建议,并且您提供了答案,请发布它 - 我会这样标记它。

谢谢,

标记

于 2013-08-15T07:41:45.950 回答
0

正如评论nemesv者正确提到的那样,问题在于

ovm.ObList = Mapper.Map<IList<Objective>, IList<ObjectiveVM>>(objectives);

ObList 不是这样的成员ObjectiveVM,您应该ObjectiveVM像这样更改:

public class ObjectiveVM
{
    public DateTime DatePeriod { get; set; }
    public IList<ObList> obList { get; set; }

    public class ObList
    {
        public int ObjectiveId { get; set; }
        public int AnalystId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string AnalystName { get; set; }
        public bool Include { get; set; }
    }
}

更新:

控制器:

public ActionResult Analyst(int id)
{
    var ovm = new ObjectiveVM { DatePeriod = new DateTime(2013, 8, 1) };
    var objectives = db.Objectives.Include(
                 o => o.Analyst).Where(x => x.AnalystId == id).ToList();

    ovm.obList = Mapper.Map<IList<Objective>, 
                 IList<ObjectiveVM.ObList>>(objectives);

    return View(ovm);
}
于 2013-08-14T15:18:31.007 回答