MVC3日在英国格式的控制器未收到 [英] MVC3 Date in UK Format not received on Controller

查看:142
本文介绍了MVC3日在英国格式的控制器未收到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此VM

public class ApplicationDTO : BaseDTO
{
    public DateTime Date { get; set; }
    public int JobId {get;set;}
    public int Status {get;set;}
    [Required]
    public string Message { get; set; }
    public string ExpertCode { get; set; }
}

我有一个隐藏字段正是如此

I have a hidden field thusly

@Html.Hidden("Date", DateTime.Now)

这小提琴手显示我被发送到服务器,因为我希望(英国格式,我在英国!)

Which fiddler shows me is sent to the server as I would expect (UK format, I'm in the UK!)

但是控制器上的时间显示作为默认分钟。日期

But on the controller the date shows as being the default min. date

难道仅仅是英国的格式?如果是这样,什么是我的最好成绩是这样?虽然目前我将其设置为当前日期,潜在地它可以被设定为任何给定的日期即

Is it just the UK format? If so, what is my best way round it? Whilst currently I am setting it to the current date, potentially it could be set to any given date i.e.

@Html.HiddenFor(x => x.Date)

我使用AJAX来提交表单,如果有差别。

I am using AJAX to submit the form, if that makes a difference.

推荐答案

如果它是一个获取MVC使用文化不变的格式默认模型绑定过程中。因此,它不接受英国的日期格式。我相信设计的原因是查询字符串,可以通过左右等于是它需要文化不变的(我不是100%确信由该逻辑)。

If it is a Get MVC uses culture invariant format by default during model binding. So it doesn't accept UK date format. I believe the design reasons are that a querystring could be passed around so therefore it needs to be culture invariant (I wasn't 100% convinced by that logic).

本文介绍了一个可能的解决方案的http://xhalent.word$p$pss.com/2011/05/14/localization-of-dates-in-asp-net-mvc/

This article covers a possible solution http://xhalent.wordpress.com/2011/05/14/localization-of-dates-in-asp-net-mvc/

我们只是确保我们不会做一个与英国的日期格式获取

We just make sure we never do a Get with a UK date format

这个问题也涵盖了问题<一href="http://stackoverflow.com/questions/4349589/globalization-problem-with-datetime-and-asp-net-mvc-3-model-binding">Globalization问题DateTime和ASP.NET MVC 3模型绑定

This questions also covers the issue Globalization problem with DateTime and ASP.NET MVC 3 Model Binding

这篇关于MVC3日在英国格式的控制器未收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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