최근에 사용자가 내 웹 사이트에 날짜를 입력하는 방법을 변경했습니다. 그것은 텍스트 상자로 사용되는 사람들이 그것을 입력하지만 의견을받은 후 사람들은 DD MM YYYY로 3 드롭 다운을 선호하는 것 같습니다. 나는 SO에서 찾은 몇 가지 코드를 사용하여 이것을 내 웹 사이트에 추가했습니다.AutoMapper Map 날짜/시간 객체
그것을 구현하려면 내가 주위에 잘못된 데이터 유효성 검사를받을 수있는 클래스를 생성했다, 내 클래스는 텍스트 상자 때이
public class DateSelector : ICustomMappings {
public DateSelector() : this(System.DateTime.MinValue) { }
public DateSelector(DateTime date) {
Year = date.Year;
Month = date.Month;
Day = date.Day;
}
[Required]
public int Year { get; set; }
[Required, Range(1, 12)]
public int Month { get; set; }
[Required, Range(1, 31)]
public int Day { get; set; }
public DateTime? DateTime {
get {
DateTime date;
if (!System.DateTime.TryParseExact(string.Format("{0}/{1}/{2}", Year, Month, Day), "yyyy/M/d", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
return null;
else
return date;
}
}
public void CreateMappings(IConfiguration configuration) {
configuration.CreateMap<DateTime, DateSelector>()
.ForMember(dest=>dest.DateTime,opt=>opt.Ignore())
.ForMember(dest => dest.Day, opt => opt.MapFrom(src => src.Day))
.ForMember(dest => dest.Month, opt => opt.MapFrom(src => src.Month))
.ForMember(dest => dest.Year, opt => opt.MapFrom(src => src.Year));
}
}
Automapper 큰 일처럼 보이지만 지금 필자 내가 얻을 드롭 다운으로 분할 다음 오류.
이진 연산자 NotEqual은 'System.DateTime'및 'System.Object'형식에 대해 정의되지 않습니다.
DateTime을 내 수업에 매핑하려고했기 때문에 내가 어떻게 생각하니, 어떻게해야합니까?
편집
: 여기 내 데이터베이스에서 데이터를 얻을 수있는 메소드를 호출 내 컨트롤러 다음 확장 메서드를 사용의 .project()public ActionResult EditYoungPerson(int youngPersonId) {
YoungPerson.EditYoungPersonViewModel editYoungPersonViewModel = new YoungPerson.EditYoungPersonViewModel();
editYoungPersonViewModel.YoungPerson = Context.GetYoungPersonDetailsForYoungPersonId(youngPersonId).Project().To<YoungPerson._YoungPersonViewModel>().Single();
return View(editYoungPersonViewModel);
}
을 여기에 스택 추적
[InvalidOperationException: The binary operator NotEqual is not defined for the types 'System.DateTime' and 'System.Object'.]
System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType binaryType, String opName, Expression left, Expression right, Boolean liftToNull) +4026223
System.Linq.Expressions.Expression.NotEqual(Expression left, Expression right) +73
AutoMapper.QueryableExtensions.Extensions.BindMappedTypeExpression(IMappingEngine mappingEngine, PropertyMap propertyMap, ExpressionResolutionResult result, MemberInfo destinationMember, IDictionary`2 typePairCount) +268
AutoMapper.QueryableExtensions.Extensions.CreateMemberBindings(IMappingEngine mappingEngine, TypePair typePair, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount) +1477
AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair, Expression instanceParameter, IDictionary`2 typePairCount) +363
AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine, TypePair typePair, IDictionary`2 typePairCount) +119
AutoMapper.QueryableExtensions.<>c__DisplayClass1`2.<CreateMapExpression>b__0(TypePair tp) +119
System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +85
AutoMapper.Internal.ConcurrentDictionaryImpl`2.GetOrAdd(TKey key, Func`2 valueFactory) +67
AutoMapper.QueryableExtensions.Extensions.CreateMapExpression(IMappingEngine mappingEngine) +322
AutoMapper.QueryableExtensions.ProjectionExpression`1.To() +111
Portal.Controllers.YoungPersonController.EditYoungPerson(Int32 youngPersonId) in c:\tfs\Portal\Dev\Portal.UI\Controllers\YoungPersonController.cs:278
lambda_method(Closure , ControllerBase , Object[]) +161
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
에게 있습니다
편집 : DataSelector 클래스를 사용하는 ViewModel을 놓쳤습니다.
public class _YoungPersonViewModel ICustomMappings {
public int YoungPersonId { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string FullName { get { return FirstName + " " + Surname; } }
public DateSelector DateOfBirth { get; set; }
public IEnumerable<SelectListItem> GenderTypeList { get; set; }
public void CreateMappings(IConfiguration configuration) {
configuration.CreateMap<Entities.Models.YoungPerson, _YoungPersonViewModel>()
.ForMember(dest => dest.DateOfBirth, opt=>opt.MapFrom(src=>src.DateOfBirth));
}
}
정확히 어디에서 오류가 발생합니까? 나는 그 결과를 초래할 수있는 위의 코드에서 어떤 점도 보지 못했다. –
스택 추적 및 내부 예외를 포함하여 전체 예외를 표시합니다. Post'ex.ToString()'. –
@JohnSaunders가 내 컨트롤러 및 스택 추적 정보를 추가했습니다. 내부 예외는 null입니다. –