I have a DateTime field in my Entity, which needs to map to 2 separate fields in the ViewModel for date and time i.e.
// 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; }
}
Looking at this example, I have the following mapping:
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 =&g开发者_高级运维t; src.EndDateTime.Date))
.ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.EndDateTime.TimeOfDay));
which is great. However, how do I reverse the mapping i.e. Mapper.CreateMap() so that it also maps when the form is POSTed?
I have done something similar to you, however I have StartDateTime as a DateTime Property on my ViewModel, and have two int properties for Hours and Minutes 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; }
}
This means that I can do the mapping thus
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)));
You should consider changing your viewmodel properties along those lines as integers will be easier to work with, however if they have to be strings, you'll need a way to parse out a DateTime object from your strings. Depending upon the format of your strings, something along these lines should work
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)));
@StanK, I have simplier mapping syntax for this case:
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)));
精彩评论