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.
While the DateTime sent has this format: 0:dd/MM/yyyy
The Get Actions expects: 0:MM/dd/yyyy
The solution (maybe)
In order to change what the Get action is expecting I'm using a Custom Model Binding.
The GET Action
public async Task<IActionResult> Details(int? id, [ModelBinder(typeof(PModelBinder))]DateTime date)
The ModelBinder class
Now here are a few things that are missing and I don't know how to complete it properly:
public class PModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
string theDate = bindingContext.HttpContext.Request.QueryString["date"];
//What should I write inside the []?
//I've tried QueryString["date"] which is the name of the parameter but it says is wrong
DateTime dt = new DateTime();
bool success = DateTime.TryParse(date); //Should I apply ParseExact? How should I do it?
if (success)
{
return new //what should I be returning here? dt?
}
}
}
I've several questions marked as comments in the code above since I'm just starting to understand Custom Model Binding. Hope anyone can give me some advice.
I'm following this article:
https://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization
But it's from 2008!!!, Although it seems valid since it's exactly the problem I'm having with my GET Action (diferent date formats)
Update: aditional information
The parameter date is defined as:
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime FechaLInicioLiq { get; set; }
and the URL build when calling that GET Action has this structure for the date parameter:
date=10%2F11%2F2017%200%3A00%3A00
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…