[英]AutoMapper Projection - date/time
I have a DateTime field in my Entity, which needs to map to 2 separate fields in the ViewModel for date and time ie 我的实体中有一个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; }
}
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 => src.EndDateTime.Date))
.ForMember(dest => dest.EndTime, opt => opt.MapFrom(src => src.EndDateTime.TimeOfDay));
which is great. 太好了 However, how do I reverse the mapping ie Mapper.CreateMap() so that it also maps when the form is POSTed?
但是,如何反转映射,即Mapper.CreateMap(),以便它也可以在表单发布时进行映射?
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 eg 我做过类似的事情,但是我在ViewModel上将StartDateTime作为DateTime属性,并为Hours和Minutes提供了两个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; }
}
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. 您应该考虑沿着这些行更改viewmodel属性,因为整数将更易于使用,但是,如果它们必须是字符串,则需要一种从字符串中解析出DateTime对象的方法。 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: @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)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.