Binding en-GB dates in a HTTP GET

2019-01-18 06:14发布

问题:

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!

回答1:

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!



回答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!