MVC3 EN-GB的获取日期 [英] MVC3 en-GB dates in Get

查看:200
本文介绍了MVC3 EN-GB的获取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个日期时间的噩梦。是总部设在英国,所以我们的日期格式为DD / MM / YYYY。这是用户键入到表单时,他们希望有一个特定的日期。我必须接受这样一个属性的表单。这显然​​是一个DateTime类型。我想送这种形式在获取控制,所以用户有一个很好的网址,他们可以节省通过对等的观点也需要一个jQuery UI日期选择绑定到这个元素也。我还需要表单元素有一定的id和class所以我真的需要渲染这这样的形式:

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" })

我在web.config中指定的英国文化变异:

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

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

看起来虽然,这里解释(<一个href=\"http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx#comments\">http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx)该歌厅的MVC忽略了文化的变种,默认为美国控制器actuion时(显然不存在一个在美国以外的,所以这是罚款,微软,咆哮以上)所附没有帮助,因为这意味着我将不得不设置这个为每一个在每一个模型日期!

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!

这意味着,如果用户键入01/05/2012我结束了05月01日这是不对的!

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

我已经看了各种解决这一问题,但没有真正适合我的需要。我需要一种方法,这样的通过GET发送的所有时间都在英国的格式为:。我们仅仅是在英国所以没有没有英国的格式将被输入。

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.

所有帮助将感激收到!

推荐答案

得到了解决,这要感谢@乔恩为指针:

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
        }

然后gloabal注册:

then register in gloabal:

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

更新

改变了这种在发帖时允许同样的问题!

altered this to allow for the same issue when POSTING!

这篇关于MVC3 EN-GB的获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆