The problem
At this point I have a problem where my Get action is trying to read a DateTime parameter in a diferent format that is sent.
<You have several issues with your model binder implementation:
date
). Use bindingContext.ModelName
instead.IValueProvider.GetValue()
with ValueProviderResult.None
.Here is sample DateTime model binder that accomplish what you need:
public class DateTimeModelBinder : IModelBinder
{
private readonly IModelBinder baseBinder = new SimpleTypeModelBinder(typeof(DateTime));
public Task BindModelAsync(ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (valueProviderResult != ValueProviderResult.None)
{
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
var valueAsString = valueProviderResult.FirstValue;
// valueAsString will have a string value of your date, e.g. '31/12/2017'
var dateTime = DateTime.ParseExact(valueAsString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
bindingContext.Result = ModelBindingResult.Success(dateTime);
return Task.CompletedTask;
}
return baseBinder.BindModelAsync(bindingContext);
}
}