我在我的ASP.NET MVC4项目中使用AutoMapper。映射2类Question和QuestionViewModel时遇到问题。在这里我的两个模型类:AutoMapper:映射元组到元组
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
酒店空值。我如何映射2类与Tuple属性?
谢谢。
您正在使用什么版本?此外,MapFrom片不是必需的,AutoMapper会自动映射GetFoo() - > Foo(不带Get的方法称为Get属性)。 –