在MVC5中指定日期格式(dd/MM/yyyy) [英] Specify Date format in MVC5 (dd/MM/yyyy)

查看:374
本文介绍了在MVC5中指定日期格式(dd/MM/yyyy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理用户输入的日期值,我想提示用户以以下格式输入日期:dd/MM/yyyy

I'm trying to handle user input for date values, I want to prompt user to input date in this format: dd/MM/yyyy

我尝试做的事情:

我阅读并实现了达林在这个问题上的答案: 在asp.net mvc 4中格式化日期时间

I read and implement the answer for Darin in this question: Format datetime in asp.net mvc 4

这是我的实现方式

在Global.asax中

In Global.asax

    (ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (!string.IsNullOrEmpty(displayFormat) && value != null)
        {
            DateTime date;
            displayFormat = displayFormat.Replace
            ("{0:", string.Empty).Replace("}", string.Empty);
            if (DateTime.TryParse(value.AttemptedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
            {
                return date;
            }
            else
            {
                bindingContext.ModelState.AddModelError(
                    bindingContext.ModelName,
                    string.Format("{0} is an invalid date format", value.AttemptedValue)
                );
            }
        }

        return base.BindModel(controllerContext, bindingContext);
    }

模型中的出发日期:

[Required(ErrorMessage = "Departure date is required")]
        [Display(Name = "Departure Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime DepartureDate { get; set; }

视图中:

 <div class="col span-1-of-2">
                            @Html.LabelFor(m => m.DesignVacation.DepartureDate)
                            @Html.EditorFor(m => m.DesignVacation.DepartureDate)

                        </div>

这是运行视图时的输出:

And this is the output when run the view:

它以month(mm)开头,但是我想要的是这种格式: dd/MM/yyyy

It starts with month(mm) but what I want is this format: dd/MM/yyyy

如何使用editorFor(以及在可能的情况下使用textboxFor)获取此格式.

How to get this format using editorFor(and if possible with textboxFor).

推荐答案

您正在通过结合使用EditorFor()[DataType][DisplayFormat]属性来生成浏览器的HTML5日期选择器.规范要求格式为yyyy-MM-dd(ISO格式).将该属性更改为:

You're generating the browser's HTML5 datepicker by using EditorFor() in conjunction with the [DataType] and [DisplayFormat] attributes. The specifications require that the format be yyyy-MM-dd (ISO format). Change the attribute to:

[Required(ErrorMessage = "Departure date is required")]
[Display(Name = "Departure Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime DepartureDate { get; set; }

,日期将以用户设备的区域性显示,这就是HTML-5日期选择器的作用.

and the date will be displayed in the culture of the user's device, which is what the HTML-5 datepicker is for.

请注意,您不需要自定义模型活页夹(日期将以ISO格式发布,并由DefaultModelBinder正确绑定).

Note that you do not need a custom model binder (the date will be posted in ISO format and will be correctly bound by the DefaultModelBinder).

但是请注意,只有Chrome和Edge支持HTML-5日期选择器,并且Firefox中会显示一个普通的文本框(例如).如果需要一致的UI,则建议您使用jquery datepicker插件(例如jquery-UI),但还需要重新配置验证器以进行客户端验证(请参阅

Note however that the HTML-5 datepicker is only supported in Chrome and Edge, and a normal textbox will be displayed in Firefox (for example). If you want a consistent UI, then it is recommended you use a jquery datepicker plugin (e.g. jquery-UI) but you will also need to reconfigure the validator for client side validation (refer to this answer for an example).

这篇关于在MVC5中指定日期格式(dd/MM/yyyy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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