我有我的實體,需要映射到視圖模型2個獨立的字段的日期和時間,即AutoMapper投影 - 日期/時間
// 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(),以便它在映射表單時也映射?
謝謝!我想在ViewModel中保持小時:分鐘,以便用戶更容易輸入/選擇時間(類似於Google日曆)。 – seekay