i am writing a mvc website that takes some information by viewmodels. this is my db poco
public class ServiceTime : BaseEntity
{
public int IdRestaurant { get; set; }
public virtual Restaurant Restaurant { get; set; }
public Src.OrderStar.Entities.Day Day { get; set; }
public string StartTime { get; set; }
public string FinishTime { get; set; }
}
and this is my view model
public class ServiceTimeModel
{
public int Id { get; set; }
public int IdRestaurant { get; set; }
public bool Monday { get; set; }
public bool Tuesday { get; set; }
public bool Wednesday { get; set; }
public bool Thursday { get; set; }
public bool Friday { get; set; }
public bool Saturday { get; set; }
public bool Sunday { get; set; }
public DateTime StartTime { get; set; }
public DateTime FinishTime { get; set; }
}
i am converting these class to each other by automapper. i can easily convert ServiceTime to ServiceTimeModel by this configuration
cfg.CreateMap<ServiceTime, ServiceTimeModel>()
.ForMember(dest => dest.Monday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Monday)))
.ForMember(dest => dest.Tuesday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Tuesday)))
.ForMember(dest => dest.Wednesday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Wednesday)))
.ForMember(dest => dest.Thursday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Thursday)))
.ForMember(dest => dest.Friday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Friday)))
.ForMember(dest => dest.Saturday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Saturday)))
.ForMember(dest => dest.Sunday, opt => opt.MapFrom(src => src.Day.HasFlag(Day.Sunday)))
.IgnoreAllNonExisting();
Bu i couldnt to the opposite of this, i mean couldn't solve how to bind ServiceTimeModel to ServiceTime
i want to bind five different days to flag enum
Any ideas?
Use ResolveUsing method and create your own implementation. For example: (it's not nice, but works)
cfg.CreateMap<ProductServiceTimeModel, ProductServiceTime>()
.ForMember(dest => dest.Day, opt => opt.ResolveUsing(src =>
{
Src.OrderStar.Entities.Day result = (src.Monday ? Day.Monday : 0) | (src.Tuesday ? Day.Tuesday: 0) | (src.Wednesday ? Day.Wednesday : 0) | (src.Thursday ? Day.Thursday: 0) | (src.Friday ? Day.Friday : 0) | (src.Saturday ? Day.Saturday : 0) | (src.Sunday ? Day.Sunday: 0);
return result;
}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.