8

I'm having a nightmare of a time with Dates. Were based in the UK, so our date format is dd/MM/yyyy. This is what users will type into the form when they want a certain date. I have a form that accepts just such a property. It's obviously a DateTime type. I want to send this form to the controller in a GET, so the user has a nice URL they can save pass on, etc. The view also needs to bind a JQuery UI datepicker to this element also. I also need the form element to have a certain id and class so I really need to render this to the form thus:

@Html.TextBoxFor(model => model.ReturnDate, new { Class = "date", id = "ReturnDate" })

I've specified a UK culture variant in the web.config:

<globalization uiCulture="en" culture="en-GB"/>

It appears though, as explained here (http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx) that when GETing a controller actuion that MVC ignores the culture variant and defaults to US (obviously no one exists outside of the US so this is fine for Microsoft, rant over) The attached doesn't help because it means I would have to set this up for every date in every model!

This means that if a user types 01/05/2012 I end up with 05/01/2012 which is wrong!

I've looked at various solutions to this issue but none really fit what I need. I need a way so that All dates sent via a GET are in UK format. We are solely based in the UK so no none UK formats will be entered.

I don't really want to create and editor unless their is a way that I can do this without a View/Viewmodel attached to this Editor as it will be come unwieldly to use this everywhere we have a date picker.

All help will be gratefully recieved!

Liam
  • 27,717
  • 28
  • 128
  • 190
  • how does the post you linked to not solve your problem? – Jon Mar 22 '12 at 11:38
  • As far as I can see I would need to do this for every querystring parameter? (bindingContext.HttpContext.Request.QueryString["theDate"];) this isn't practicle. – Liam Mar 22 '12 at 11:40
  • 1
    if you implement the custom model binder, it will affect how dates come into your app throughout the whole app, in querystrings, form data, cookies, etc – Jon Mar 22 '12 at 11:43
  • yes your right, but how do I get the dates out of the ModelBindingContext? the example included just grabs A date out of the querystring. I want to affect ALL dates regardless. – Liam Mar 22 '12 at 11:50
  • Or am I mmissing somethign here? – Liam Mar 22 '12 at 11:51
  • 1
    ModelBindingContext has a ModelName property - you can inspect that and then look whereever you want to find some data with the corresponding name, and then apply whatever parsing logic you like... – Jon Mar 22 '12 at 11:57
  • I see what your getting at! So what your saying is read the name out of the bindingcontext, read the value our of the contorller context, do the logic! whoop whoop – Liam Mar 22 '12 at 12:08

2 Answers2

10

Got a solution, thanks to @Jon for the pointers:

public class GBDateModelBinder : IModelBinder
{

    #region IModelBinder Members

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        string dateString = controllerContext.HttpContext.Request.QueryString[bindingContext.ModelName];
        if (string.IsNullOrEmpty(dateString))
                 dateString = controllerContext.HttpContext.Request.Form[bindingContext.ModelName];
        DateTime dt = new DateTime();
        bool success = DateTime.TryParse(dateString, CultureInfo.GetCultureInfo("en-GB"), DateTimeStyles.None, out dt);
        if (success)
        {
            return dt;
        }
        else
            {
            return null;
        }
    }

    #endregion
}

then register in global.asax:

ModelBinders.Binders.Add(typeof(DateTime), new GBDateModelBinder());

UPDATE

altered this to allow for the same issue when POSTing!

Liam
  • 27,717
  • 28
  • 128
  • 190
  • Where do i put this GBDateModelBinder : IModelBinder class? – KayGee Aug 19 '16 at 07:33
  • It doens't matter? It's a class. So long as you can access it in the globals.asax to register it where you put it is up to you. – Liam Aug 19 '16 at 07:52
2

In addition to Liam's answer:

For those who are still having this issue (!) and are trying to bind to a Nullable DateTime, make sure you register the correct type in global.asax:

ModelBinders.Binders.Add(typeof(DateTime), new GBDateModelBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new GBDateModelBinder());

Had me stumped for 15 minutes!

Nick
  • 685
  • 12
  • 27