94 lines
3.5 KiB
C#
94 lines
3.5 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
|
|
|
|
namespace gehGassi.Web.Helper
|
|
{
|
|
public class DateTimeModelBinder : IModelBinder
|
|
{
|
|
private static readonly string[] DateTimeFormats = { "yyyyMMdd'T'HHmmss.FFFFFFFK", "yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK" };
|
|
|
|
public Task BindModelAsync(ModelBindingContext bindingContext)
|
|
{
|
|
if (bindingContext == null)
|
|
throw new ArgumentNullException(nameof(bindingContext));
|
|
|
|
var stringValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).FirstValue;
|
|
|
|
if (bindingContext.ModelType == typeof(DateTime?) && string.IsNullOrEmpty(stringValue))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(null);
|
|
return Task.CompletedTask;
|
|
}
|
|
if (bindingContext.ModelType == typeof(DateTimeOffset?) && string.IsNullOrEmpty(stringValue))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(null);
|
|
return Task.CompletedTask;
|
|
}
|
|
if (bindingContext.ModelType == typeof(DateOnly?) && string.IsNullOrEmpty(stringValue))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(null);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
if (bindingContext.ModelMetadata.UnderlyingOrModelType == typeof(DateOnly))
|
|
{
|
|
if (DateOnly.TryParse(stringValue, null, DateTimeStyles.RoundtripKind, out var dateOnly))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(dateOnly);
|
|
}
|
|
else
|
|
{
|
|
ModelBindingResult.Failed();
|
|
}
|
|
}
|
|
if (bindingContext.ModelMetadata.UnderlyingOrModelType == typeof(DateTimeOffset))
|
|
{
|
|
if (DateTimeOffset.TryParse(stringValue, null, DateTimeStyles.RoundtripKind, out var dateTimeOffset))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(dateTimeOffset);
|
|
}
|
|
else
|
|
{
|
|
ModelBindingResult.Failed();
|
|
}
|
|
}
|
|
else if (bindingContext.ModelMetadata.UnderlyingOrModelType == typeof(DateTime))
|
|
{
|
|
if (DateTime.TryParse(stringValue, null, DateTimeStyles.RoundtripKind, out var date))
|
|
{
|
|
bindingContext.Result = ModelBindingResult.Success(date);
|
|
}
|
|
else
|
|
{
|
|
ModelBindingResult.Failed();
|
|
}
|
|
}
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
public class DateTimeModelBinderProvider : IModelBinderProvider
|
|
{
|
|
public IModelBinder GetBinder(ModelBinderProviderContext context)
|
|
{
|
|
if (context == null)
|
|
throw new ArgumentNullException(nameof(context));
|
|
|
|
if (context.Metadata.UnderlyingOrModelType == typeof(DateTime))
|
|
return new BinderTypeModelBinder(typeof(DateTimeModelBinder));
|
|
|
|
if (context.Metadata.UnderlyingOrModelType == typeof(DateTimeOffset))
|
|
return new BinderTypeModelBinder(typeof(DateTimeModelBinder));
|
|
|
|
if (context.Metadata.UnderlyingOrModelType == typeof(DateOnly))
|
|
return new BinderTypeModelBinder(typeof(DateTimeModelBinder));
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|