English 中文(简体)
日记和 c# 模型之间日期格式不相近
原标题:Date formats inconsistant between javascript and c# models

*%%%UPDATE

我刚刚意识到 除非我用火狐,否则这个日期 根本就没有在发布时被数据固定起来。

为什么浏览器会影响服务器上的数据绑定?

我觉得我在吃疯狂药丸


我的问题是,我有一个 JQuery 日期采集器, 似乎格式格式正确(mm/dd/yyyyyyy), 但当数值被张贴到服务器上时, 月份和天数会被转换 。

日期属性会像这样添加到页面上( 它被隐藏了, 以便能在张贴时绑定 。 如果它是一个简单的文本框, 问题是一样的 ) :

<input type="hidden" name="SearchByDate" id="SearchByDate" value="<%: ((DateTime)Model.SearchByDate).ToString(Web.Common.Config.GetDateFormatBase) %>" />

在我的观点模型中,我指定日期格式如下:

    [DisplayFormat(DataFormatString = Common.Config.GetDateFormat, 
        ApplyFormatInEditMode = true)]
    public DateTime? SearchByDate { get; set; }

我的雅瓦记本日期选手是像这样格式的:

        $("#appointmentDateDiv").datepicker({
------>     dateFormat: "<%= Web.Common.Config.GetDateFormatJavascript %>",
            changeMonth: true,
            onSelect: function (dateText, inst) {
                $("#SearchByDate").val(dateText);
                $("form").submit();
            }
        });

我实际上在这种普通类中 将格式简化为:

public const string GetDateFormatBase = @"MM/dd/yyyy";
public const string GetDateFormat = @"{0:" + GetDateFormatBase + "}"; //This is for annotations
public const string GetDateFormatJavascript = "mm/dd/yy";

As you can see, the formats are all the sam:month day year. Everything looks good when I first load a page like this, so the datepicker lets me select a date in the right format. Then I post and if I step through my code everything still looks fine. I even re-format the date to make sure before I return to the view. But the month and day always get switched.

最奇怪的是,在火狐中,每件事物都很管用。它在IE,Chrome,和歌剧中都失败了。但我不明白为什么...

最佳回答

谢谢 帮了大忙

@JohnKoerner,你说的对,这是一个18n问题。我的Firefox被设置为 en-US, 我的其他浏览器也被设置为 en-US 。 我并不认为它们是不同的日期格式, 但是是。 这在模型约束期间造成了问题。 我的解决办法是使用异端文化在自定义的模型捆绑器中解析所有日期。 我的想法来自

但是,我已经用了一个不同的模型,从"http://www.hanselman.com/blog/splitingDateTime Unitime 测试ASPNETMMVCUSTOMModelBinders.aspx" rel =“nofollow”这里 。我修改了相关的代码,像这样。

retVal = (Nullable<T>)value.ConvertTo(typeof(T), CultureInfo.InvariantCulture);

我们的约会时间在哪里?

另一个问题就是确保注册“日期时间”和“日期时间”的自定义模型集?

ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());
问题回答

您可以在隐藏字段中使用字符串。 格式化 :

<input type="hidden" name="SearchByDate" id="SearchByDate" value="<%:
String.Format("{0:MM/dd/yyyy}",Model.SearchByDate) %>" />




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签