【发布时间】:2014-02-28 14:20:33
【问题描述】:
我在我的 ASP.NET MVC4 项目中使用 AutoMapper。映射 2 类 Question 和 QuestionViewModel 时出现问题。这是我的两个模型类:
public class Question
{
public int Id { get; set; }
public string Content { get; set; }
public Tuple<int, int> GetVoteTuple()
{
"some code here"
}
}
public class QuestionViewModel
{
public int Id { get; set; }
public string Content { get; set; }
public Tuple<int, int> VoteTuple { get; set; }
}
这是我的控制器代码:
public class QuestionController: Controller
{
public ActionResult Index(int id)
{
Question question = Dal.getQuestion(id);
Mapper.CreateMap<Question, QuestionViewModel>()
.ForMember(p => p.VoteTuple,
m => m.MapFrom(
s => s.GetVoteTuple()
));
QuestionViewModel questionViewModel =
Mapper.Map<Question, QuestionViewModel>(question);
return View(questionViewModel);
}
}
当我运行此代码时,QuestionViewModel 中的 VoteTuple 属性具有空值。如何使用 Tuple 属性映射 2 个类?
谢谢。
【问题讨论】:
-
你用的是什么版本?此外,MapFrom 部分不是必需的,AutoMapper 自动映射 GetFoo() -> Foo(称为 Get 到属性的方法,没有 Get)。
标签: c# asp.net asp.net-mvc-4 automapper