public async Task ShouldBindDateTimeAsModel(int?year, int?month, int?day, bool isValid)
        {
            var formCollection = new FormCollection(new Dictionary <string, StringValues>()
            {
                { "Day", day.ToString() },
                { "Month", month.ToString() },
                { "Year", year.ToString() },
            });

            var binder = new DateTimeModelBinder();

            var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture);

            var context = GetBindingContext(vp, typeof(DateTime));

            await binder.BindModelAsync(context);

            context.ModelState.IsValid.Should().Be(isValid);

            if (isValid)
            {
                var dateValue = (DateTime)context.Result.Model;

                dateValue.Date.Day.Should().Be(day);
                dateValue.Date.Month.Should().Be(month);
                dateValue.Date.Year.Should().Be(year);
                dateValue.TimeOfDay.Hours.Should().Be(0);
                dateValue.TimeOfDay.Minutes.Should().Be(0);
                dateValue.TimeOfDay.Seconds.Should().Be(0);
            }
            else
            {
                context.ModelState.ErrorCount.Should().Be(1);
            }
        }
Exemple #2
0
    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        await _dateTimeModelBinder.BindModelAsync(bindingContext);

        if (bindingContext.Result.IsModelSet && bindingContext.Result.Model is DateTime dateTime)
        {
            bindingContext.Result = ModelBindingResult.Success(_clock.Normalize(dateTime));
        }
    }
        public async Task ShouldNotBindIncompleteValueCollectionForDateTime_MissingYear()
        {
            var formCollection = new FormCollection(new Dictionary <string, StringValues>()
            {
                { "Day", "1" },
                { "Month", "12" },
            });

            var binder = new DateTimeModelBinder();

            var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture);

            var context = GetBindingContext(vp, typeof(DateTime));

            await binder.BindModelAsync(context);

            context.Result.Model.Should().BeNull();
        }
        /// <inheritdoc />
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (valueProviderResult.Values.Count == 1)
            {
                var dateTimeString = valueProviderResult.FirstValue;
                // Mark Played Item.
                if (DateTime.TryParseExact(dateTimeString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var dateTime))
                {
                    bindingContext.Result = ModelBindingResult.Success(dateTime);
                }
                else
                {
                    return(_defaultModelBinder.BindModelAsync(bindingContext));
                }
            }

            return(Task.CompletedTask);
        }
        public async Task TestModelBinderDateTime()

        {
            var binder = new DateTimeModelBinder();

            var formCollection = new FormCollection(
                new Dictionary <string, StringValues>()
            {
                { "startDate", new StringValues("14.01.2008") },
            });
            var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture);

            var context = GetBindingContext(vp, typeof(DateTime?));

            await binder.BindModelAsync(context);

            var resultModel = context.Result.Model as DateTime?;

            Assert.NotNull(resultModel);
            Assert.True(((DateTime)resultModel).Day == 14);
            //TODO asserts
        }