Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC3:强制控制器使用日期格式dd/mm/yyyy_C#_Asp.net_Asp.net Mvc 3 - Fatal编程技术网

C# ASP.NET MVC3:强制控制器使用日期格式dd/mm/yyyy

C# ASP.NET MVC3:强制控制器使用日期格式dd/mm/yyyy,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,基本上,我的datepicker使用英国格式的dd/mm/yyyy。但当我提交表单时,ASP.net显然使用的是US格式。 (如果少于12天,则仅接受天,即认为是月份。) 有没有办法强迫它承认某种方式 奇怪的是,其他插入方法似乎能识别正确的格式 “对于方法System.Web.Mvc.ActionResult索引(System.DateTime),参数字典包含不可为空的类型System.DateTime的参数ViewDate的空条目Mysite.Controllers.RoomBookingsC

基本上,我的datepicker使用英国格式的
dd/mm/yyyy
。但当我提交表单时,ASP.net显然使用的是US格式。 (如果少于12天,则仅接受天,即认为是月份。)

有没有办法强迫它承认某种方式

奇怪的是,其他插入方法似乎能识别正确的格式


“对于方法
System.Web.Mvc.ActionResult索引(System.DateTime),参数字典包含不可为空的类型
System.DateTime
的参数
ViewDate
的空条目
Mysite.Controllers.RoomBookingsController中的
。可选参数必须是引用类型、可为null的类型或声明为可选参数。“

您需要为日期时间使用自定义ModelBinder。我和您有同样的问题。

您是否尝试将当前区域性设置为en GB

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
     base.Initialize(requestContext);

     CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-GB");

     Thread.CurrentThread.CurrentCulture = cultureInfo;
     Thread.CurrentThread.CurrentUICulture = cultureInfo;                    
 }
基本上,我的日期选择器使用英国的dd/mm/yyyy格式

初学者的错误。它应该使用浏览器设置为的任何格式。问题不在于格式,而在于客户端和服务器之间的格式不同。服务器应该发出代码,根据协商的区域设置格式化日期,然后服务器也使用该区域设置解析日期

但当我提交表单时,ASP.NET显然使用的是US格式

没有。这是说,当我吃香料时,它总是盐,然后你总是吃盐。您的服务器接受当前协商的区域性,除非您修改设置,否则该区域性是在客户端和服务器之间协商的。当线程应该进行解析时,请检查线程的当前区域性,以查看其设置目的。

请阅读。它很好地解释了正在发生的事情以及为什么它会这样工作

我知道每个使用该网站的人都在英国,因此我可以安全地覆盖默认的
DateTime
model活页夹:

public class DateTimeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var date = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;

        if (String.IsNullOrEmpty(date))
            return null;

        bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName));
        try
        {
            return DateTime.Parse(date);
        }
        catch (Exception)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, String.Format("\"{0}\" is invalid.", bindingContext.ModelName));
            return null;
        }
    }
}
你可以做到:

  • 全局(在应用程序_Start()下的global.asax中):

  • 对于一种方法:

        public ActionResult TimeTable([Binder(typeof(DateTimeModelBinder)]DateTime ViewDate)
    
  • 对于自定义模型类-啊,不,不可能,因为您使用struct DateTime;-)


啊,很抱歉,我无法向Adams post添加评论-它基于他的代码。

来自我的BindingTools for Binding DateTime?(可为空),基于一些书籍样本-Pro MVC3

    public static DateTime? GetValueNDateTime(ModelBindingContext context, string searchPrefix, string key, string format)
    {
        ValueProviderResult vpr = context.ValueProvider.GetValue(searchPrefix + key);
        DateTime outVal;
        if (DateTime.TryParseExact(vpr.AttemptedValue, format, null, System.Globalization.DateTimeStyles.None, out outVal))
        {
            return outVal;
        }
        else
        {
            return null;
        }
    }

它使用精确解析,因此您应该不会对解析的日期有任何问题。

这会导致什么问题?Global.asax.cs?有几个地方,我的示例是重写控制器初始化方法,但您可以将其放在View.InitializeCulture或Global.asax中。看这里;除非
DateTime
来自表单集合,否则这将不起作用。如果它来自查询字符串或路线数据,则区域性将被忽略。我将其放在预订控制器的顶部,但它仍执行相同的操作:(当然更愿意在Global.asax.csOk中的应用程序_BeginRequest()中添加此选项,因此,如果datetimepicker使用浏览器集选项,则无论datetimepicker的格式如何,它仍将以实际表示的datetime形式出现在asp.net代码中?这取决于您是如何编码的。也可以作为写得不好的datetime picker的字符串出现。您说“我的日期时间选择器”。修复它;)哎哟-可能完全不知道与服务器协商的设置。检查看看oyu必须如何在页面中初始化;)基本上告诉它使用协商的格式。然后它们匹配。我目前已将jquery datetimepicker重写为使用UK格式。我的PC是UK格式,但它在post to C#代码中仍以我们的身份读取。即使以下选项将覆盖:(我仍然不知道如何修复它。通过阅读提供的,它对于不同的情况更有意义。这篇文章立即调用了一个格式不同的视图。其他文章存储到DB的位置,因此被正确识别。好的,那么我将把这个代码放在哪里/我将如何引用它,以供使用。谢谢上面的代码位于Binders目录中它自己的文件中。然后,您需要在
应用程序_Start()
中使用类似
ModelBinders.Binders.Add(typeof(DateTime),new DateTimeModelBinder()的内容注册binder你的代码一直在倒转DATETIME。所以是英国,然后是美国,然后是英国等。等等,它并没有解决显示问题。显然,美国和英国的冲突仍然在使用,造成了奇怪的影响。IM使用<代码> Addid(1)< /Cord>,并且保持反相。最后,我可以添加注释。考虑使用DATETIME.TyPARSESECK()。,您可以在那里指定日期时间格式(“{0:dd/MM/yyyy}”注意:如果您希望使用MVC3保存,还需要更改不引人注目的javascript。否则它甚至不允许您发布。
    public ActionResult TimeTable([Binder(typeof(DateTimeModelBinder)]DateTime ViewDate)
    public static DateTime? GetValueNDateTime(ModelBindingContext context, string searchPrefix, string key, string format)
    {
        ValueProviderResult vpr = context.ValueProvider.GetValue(searchPrefix + key);
        DateTime outVal;
        if (DateTime.TryParseExact(vpr.AttemptedValue, format, null, System.Globalization.DateTimeStyles.None, out outVal))
        {
            return outVal;
        }
        else
        {
            return null;
        }
    }