我的实体中有一个DateTime字段,需要映射到ViewModel中的2个单独字段的日期和时间,即
// Entity
public partial class Event
{
public Int64 Id { get; set; }
public String Title { get; set; }
public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
}
// ViewModel
public class EventAddEditViewModel
{
public Int64 Id { get; set; }
public String Title { get; set; }
public String StartDate { get; set; }
public String EndDate { get; set; }
public String StartTime { get; set; }
public String EndTime { get; set; }
}
查看this example,我有以下映射:
Mapper.CreateMap<Event, EventAddEditViewModel>()
.ForMember(dest => dest.StartDate, opt => opt.MapFrom(src => src.StartDateTime.Date))
.ForMember(dest => dest.StartTime, opt => opt.MapFrom(src => src.StartDateTime.TimeOfDay))
.ForMember(dest => dest.EndDate, opt => opt.MapFrom(src => src.EndDateTime.Date))
.ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.EndDateTime.TimeOfDay));
这很棒。但是,如何反转映射,即Mapper.CreateMap(),以便它在表单POST时映射?
答案 0 :(得分:2)
我做了类似的事情,但是我的ViewModel上有StartDateTime作为DateTime属性,并且有几个小时和分钟的int属性 e.g。
// ViewModel
public class EventAddEditViewModel
{
public Int64 Id { get; set; }
public String Title { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int StartHour { get; set; }
public int EndHour { get; set; }
public int StartMinute { get; set; }
public int EndMinute { get; set; }
}
这意味着我可以进行映射
Mapper.CreateMap<Event, EventAddEditViewModel>()
.ForMember(dest => dest.StartDate, opt => opt.MapFrom(src => src.StartDateTime.Date))
.ForMember(dest => dest.StartHour, opt => opt.MapFrom(src => src.StartDateTime.Hour))
.ForMember(dest => dest.StartMinute, opt => opt.MapFrom(src => src.StartDateTime.Minute))
.ForMember(dest => dest.EndDate, opt => opt.MapFrom(src => src.StartDateTime.Date))
.ForMember(dest => dest.EndHour, opt => opt.MapFrom(src => src.EndDateTime.Hour))
.ForMember(dest => dest.EndMinute, opt => opt.MapFrom(src => src.StartDateTime.Minute));
Mapper.CreateMap<EventAddEditViewModel, Event>()
.ForMember(dest => dest.StartDateTime, opt => opt.MapFrom(src => new DateTime(src.StartDate.Year, src.StartDate.Month, src.StartDate.Day, src.StartHour, src.StartMinute, 0)))
.ForMember(dest => dest.EndDateTime, opt => opt.MapFrom(src => new DateTime(src.EndDate.Year, src.EndDate.Month, src.EndDate.Day, src.EndHour, src.EndMinute, 0)));
您应该考虑沿着这些行更改viewmodel属性,因为整数将更容易使用,但是如果它们必须是字符串,则需要一种方法从字符串中解析出DateTime对象。根据字符串的格式,这些行中的某些内容应该可以正常工作
Mapper.CreateMap<EventAddEditViewModel, Event>()
.ForMember(dest => dest.StartDateTime, opt => opt.MapFrom(src => DateTime.Parse( src.StartDate + " " + src.StartTime)))
.ForMember(dest => dest.EndDateTime, opt => opt.MapFrom(src => DateTime.Parse(src.EndDate + " " + src.EndTime)));
答案 1 :(得分:1)
@StanK,我对这种情况有简单的映射语法:
Mapper.CreateMap<EventAddEditViewModel, Event>()
.ForMember(dest => dest.StartDateTime, opt => opt.MapFrom(src => src.StartDate.Add(src.StartTime.TimeOfDay)))
.ForMember(dest => dest.EndDateTime, opt => opt.MapFrom(src => src.EndDate.Add(src.EndTime.TimeOfDay)));