public DateMonthYearModelBinder(string dateStringFormat, Forms.DateMonthYearAttribute.DayValueOption dayValue) { if (string.IsNullOrWhiteSpace(dateStringFormat)) { throw new ArgumentNullException(nameof(dateStringFormat), "A date format string must be supplied"); } this.dateStringFormat = dateStringFormat; this.dayValue = dayValue; }
public async Task ShouldBindDateTimeAsModelWithCustomDateFormatStringAndDayValue(string dateFormatString, string?year, string?month, Forms.DateMonthYearAttribute.DayValueOption dayValue, bool isValid, string expectedDateString) { var formCollection = new FormCollection(new Dictionary <string, StringValues>() { { "Month", month?.ToString() }, { "Year", year?.ToString() }, }); var binder = new DateMonthYearModelBinder(dateFormatString, dayValue); var vp = new FormValueProvider(BindingSource.Form, formCollection, CultureInfo.CurrentCulture); var context = GetBindingContext(vp, typeof(string)); await binder.BindModelAsync(context); context.ModelState.IsValid.Should().Be(isValid); if (isValid) { var dateValueString = (string)context.Result.Model; dateValueString.Should().Be(expectedDateString); } else { context.ModelState.ErrorCount.Should().Be(1); } }