Consider setting the Date property for the view model: string
Then, either write a utility function to handle the mapping between the type of the view model and the type of domain model:
public static MyCriteria MapMyCriteriaViewModelToDomain(MyCriteriaViewModel model){ var date = Convert.ToDateTime(model.Date.Substring(0,2) + "/" model.Date.Substring(2,2) + "/" model.Date.Substring(4,2)); return new MyCriteria { ID = model.ID, Date = date }; }
or use a tool like AutoMapper , for example:
at Global.asax
//if passed as MMDDYYYY: Mapper.CreateMap<MyCriteriaViewModel, MyCriteria>(). .ForMember( dest => dest.Date, opt => opt.MapFrom(src => Convert.ToDateTime(src.Date.Substring(0,2) + "/" src.Date.Substring(2,2) + "/" src.Date.Substring(4,2))) );
and in the controller:
public ActionResult MyAction(MyCriteriaViewModel model) { var myCriteria = Mapper.Map<MyCriteriaViewModel, MyCriteria>(model);
In this example, AutoMapper might not seem to provide any added value. This value occurs when you configure several or more mappings to objects that usually have more properties than this example. CreateMap automatically maps properties to the same name and type, so it saves a lot of input and a lot of DRYer.
source share