Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Richard Eyres 98 posts 580 karma points
    Nov 09, 2016 @ 15:23
    Richard Eyres
    0

    Surface controller UK date binding

    I currently have a custom form posting to a surface controller (form could not be done in Umbraco Forms due to complexity of the form). One of these fields being posted is a Date of Birth. This is a UK site, so the date being inputted is in dd/MM/yyyy.

    When binding in the Surface Controller, the modelstate errors, stating the date is not in the correct format - as it is using the US culture. I would like to change this to use UK (or en-GB) culture across my site.

    I have checked various other sites and solutions over the internet, and i can get non of the them to work, the US culture is still ruling on the binds.

    Has anyone else had the culture issue and how was it resolved?

    I have tried setting the culture in Web.config

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

    Tried a base Surface Controler

    public class BaseSurfaceController : SurfaceController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var culture = new CultureInfo("en-GB"); 
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
    
            base.OnActionExecuting(filterContext);
        }
    }
    

    Some others i can not implement due to it not being available (as it looks like it is amends to the global.asax file , which is being managed by Umbraco now).

    Any guidance greatly appreciated.

  • Edward Aston 3 posts 73 karma points
    Jun 13, 2018 @ 11:17
    Edward Aston
    0

    Hi, Did you ever find a solution to this, it's driving me mad! Thanks, Ed

  • Richard Eyres 98 posts 580 karma points
    Jun 13, 2018 @ 11:31
    Richard Eyres
    1

    Hello

    I feel your pain.

    I didn't find a good solution, just a work around. In my view i had the standard datepicker. This was purely for the front end, and when the date was selected/changed, i populated another hidden field, which was submitted to the surface controller to work on. This allowed the UK date to be displayed to the user, and the date in the format that .net was happy to work with.

    Hope that helps a little for you.

  • Andy Neil 7 posts 80 karma points c-trib
    Jun 13, 2018 @ 13:53
    Andy Neil
    2

    Hi Richard/Edward,

    I had this issue some time ago and it was a right pain! I did manage to solve it - my solution may not be the most elegant though. I used a custom ModelBinder at the input param level.

    /// <summary>
    /// Allows to pass date using get using current server's culture instead of invariant culture.
    /// </summary>
    public class UkDateTimeModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            var date = valueProviderResult.AttemptedValue;
    
            if (String.IsNullOrEmpty(date))
            {
                return null;
            }
    
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);
    
            try
            {
                // Parse DateTimeusing current culture.
                return DateTime.Parse(date);
            }
            catch (Exception)
            {
                bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid.", bindingContext.ModelName));
                return null;
            }
        }
    }
    

    Then when you call the Ajax method you can do this:

    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
        public JsonResult DateMethod([ModelBinder(typeof(UkDateTimeModelBinder))] DateTime? DateValue)
        {
            return //whatever you want
        }
    

    Cheers,

    Andy

  • Edward Aston 3 posts 73 karma points
    Jun 14, 2018 @ 10:30
    Edward Aston
    0

    Thanks both.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 14, 2018 @ 10:51
    Dan Diplo
    1

    I know this might seem obvious, but sometimes we overlook the obvious...

    Have you gone into Settings > Languages and added English (United Kingdom) as the default language? ie. replace English US. You can also bind this to the site via the Culture and Hostnames setting on the home node, but if there is only one language set in Umbraco then it should use that by default.

    This should ensure your site uses UK culture.

  • Edward Aston 3 posts 73 karma points
    Jun 14, 2018 @ 11:06
    Edward Aston
    0

    Can't believe this is the answer!!! We've spent a couple of days on this... Thanks

  • Andy Neil 7 posts 80 karma points c-trib
    Jun 14, 2018 @ 11:12
    Andy Neil
    0

    Hi Dan,

    I thought the same but MVC always seems to bind using culture invariant when it's passed in such a way as a parameter to the SurfaceController.

    Took me ages to find this out, even with the web config, hostnames etc cultures set correctly it refused to work.

    Here's a good post about the issue with a bit more info.

    This was a fair while back so maybe the core culture stuff in Umbraco has solved this now.

Please Sign in or register to post replies

Write your reply to:

Draft