2011-09-26 62 views
1

我有我的實體,需要映射到視圖模型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(),以便它在映射表單時也映射?

回答

2

我已經做了類似於你的事情,但是我的StartDateTime作爲我的ViewModel上的DateTime屬性,並且有兩個int屬性,分別爲小時和分鐘

// 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))); 

你應該考慮改變您的視圖模型的屬性沿着這些線路爲整數會更容易的工作,但是如果他們必須是字符串,你需要一種方法從你的字符串中解析出一個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))); 
+0

謝謝!我想在ViewModel中保持小時:分鐘,以便用戶更容易輸入/選擇時間(類似於Google日曆)。 – seekay

1

@StanK,我有simplier映射語法這種情況:

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)));