コード例 #1
0
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException(nameof(bindingContext));
        }
        var valueProviderResult = bindingContext.ValueProvider
                                  .GetValue(bindingContext.ModelName);

        if (string.IsNullOrEmpty(valueProviderResult.FirstValue))
        {
            return(null);
        }
        DateTime datetime;

        if (DateTime.TryParse(valueProviderResult.FirstValue, null, DateTimeStyles.AdjustToUniversal, out datetime))
        {
            bindingContext.Result =
                ModelBindingResult.Success(UserCulture.GetUtcTime(datetime));
        }
        else
        {
            // TODO: [Enhancement] Could be implemented in better way.
            bindingContext.ModelState.TryAddModelError(
                bindingContext.ModelName,
                bindingContext.ModelMetadata
                .ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor(
                    valueProviderResult.ToString(), nameof(DateTime)));
        }
        return(Task.CompletedTask);
    }
コード例 #2
0
        public Task BindModelAsync(ModelBindingContext context)
        {
            if(context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var valueProviderResult = context.ValueProvider.GetValue(context.ModelName);

            if(string.IsNullOrWhiteSpace(valueProviderResult.FirstValue))
            {
                return null;
            }

            DateTime datetime;
            if(DateTime.TryParse(valueProviderResult.FirstValue, null, DateTimeStyles.AdjustToUniversal, out datetime))
            {
                context.Result = ModelBindingResult.Success(_userCulture.GetUtcTime(datetime));
            }       
            else
            {
                context.ModelState.TryAddModelError(
                    context.ModelName,
                    context.ModelMetadata
                        .ModelBindingMessageProvider
                        .AttemptedValueIsInvalidAccessor(valueProviderResult.ToString(), nameof(DateTime)));
            }

            return Task.CompletedTask;
        }
コード例 #3
0
 public void ReturnUtcTimeBasedOnUserLocalTime()
 {
     Assert.Equal(_utcTime, _sut.GetUtcTime(_userLocalTime));
 }