Search code examples
c#asp.net-mvcdatetimecultureinfodefaultmodelbinder

DateTime.Parse not working on an MVC custom binder


I'm getting a DateTime from an implicit binding at an Action in a .NET MVC web application. The problem is that i'm getting the date in format "MM/dd/yyyy" while i'm sending it through a query string with Ajax in format "dd/MM/yyyy".

I know this is a known issue of the .NET MVC Binder when using the GET protocol and not POST, so i did implement a custom binder to parse the date to the correct format. Here is the code:

public class SearchVMBinder:DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        SearchVM result = (SearchVM)base.BindModel(controllerContext, bindingContext);
        try
        {                
            result.Date = DateTime.Parse(result.Date.ToString(),CultureInfo.GetCultureInfo("es-ES"));
        }
        catch(Exception e)
        {
            NLog.LogManager.GetCurrentClassLogger().Error("Error al hacer el Bind específico de SearchVM. ", e);
        }

        return result;
    }
}

But, with that code the Parse is not working, it just does nothing. I'm testing it with a date like "01/04/2014 11:37:00" (april) and i'm getting at the "result.Date" the date "04/01/2014 11:37:00" (january), before and afert parsing.

So, the question is: Why is the method "DateTime.Parse" not parsing the date correctly?

UPDATE:

Here is the code of the SearchVM:

[ModelBinder(typeof(SearchVMBinder))]
public class SearchVM
{
    public DateTime Date { get; set; }
    public string StudyCaseNumber { get; set; }
    public string PatientNumber { get; set; }
    public string PatientName { get; set; }
    public string PatientFamilyName { get; set; }
    public string PatientMothersMaidenName { get; set; }
    public string DoctorName { get; set; }
    public string RoomName { get; set; }
    public E_OrderedBy OrderBy { get; set; }

}

and here the header of the controller's action:

public ActionResult ListSearch(SearchVM searchFilter)

Thank you.


Solution

  • Ok, for some reason the "Parse" method did not interpret properly the incoming DateTime format, which was "MM/dd/yyyy H:mm:ss", so i had to use the "ParseExact" method to specify it.

    Now, this works perfect for a spanish culture info:

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            SearchVM result = (SearchVM)base.BindModel(controllerContext, bindingContext);
            try
            {
                if (result != null)
                {
                    if (result.NullableDate != null)
                    {                                      
                        result.NullableDate = DateTime.ParseExact(result.NullableDate.ToString(), "MM'/'dd'/'yyyy H:mm:ss", new CultureInfo("es-ES"));
                    }
                }
    
            }
            catch(Exception e)
            {
                NLog.LogManager.GetCurrentClassLogger().Error("Error al hacer el Bind específico de SearchVM. ", e);
            }
    
            return result;
        }