返回的DatePicker值格式错误 [英] Returned DatePicker Value in Wrong Format

查看:145
本文介绍了返回的DatePicker值格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Twitter Bootstrap Datepicker https://github.com/eternicode/bootstrap-datepicker

I'm using Twitter Bootstrap Datepicker https://github.com/eternicode/bootstrap-datepicker within my MVC 5 application. It's hooked up to a textbox within a View called Selector

视图选择器

@model STAR.UI.ViewModels.ReportSelectorViewModel

<script>
    $(document).ready(function () {
        $('.datepicker').datepicker({
            autoclose: true,
            format: "dd/mm/yyyy",
            daysOfWeekDisabled: "5,6"
        });
    });
</script>

@Html.TextBoxFor(m => m.StartDate, new { @class = "datepicker" })

当用户选择一个日期时,它将以英国日期格式dd/mm/yyyy在英国显示.这是对的.然后,用户可以单击提交按钮,该按钮在另一页上显示数据.在该页面中,有一个后退按钮,可将用户返回到选择器视图",他们可以在其中选择另一个日期.但是,我也希望当用户单击后退"按钮时,他们当前的日期选择也将被传递回文本框中.

When the user selects a date it displays in the textbox in a UK date format dd/mm/yyyy. This is correct. The user can then hit a submit button which displays data on another page. Within that page there is a back button which returns the user back to the Selector View where they can select another date. However, I'd also like when the user hits the back button that their current date selection is also passed back into the textbox.

后退按钮

@Html.ActionLink("<<< Go Back", "Selector", "Reports", new { startDate = Model.StartDate }, new { @class = "btn btn-outline btn-primary" })

后退按钮将选定的StartDate传递回Controller中的Selector Action,然后将此值分配给ViewModel,然后将ViewModel传递回Selector View.

The back button passes the selected StartDate back to the Selector Action within my Controller and I then assign this value to the ViewModel, and the ViewModel is then passed back to the Selector View.

选择器操作(在控制器中)

public ActionResult Selector(DateTime? startDate)
{
  ReportSelectorViewModel model = new ReportSelectorViewModel();
  model.StartDate = startDate;
  return View(model);
}

ReportSelectorViewModel中的StartDate属性是DateTime类型吗?

The StartDate property in the ReportSelectorViewModel is of type DateTime?

问题在于,当日期返回到文本框时,它采用的是mm/dd/yyyy格式,而不是我需要的英国格式dd/mm/yyyy.

The problem is that when the date is returned to the textbox, it is in the format of mm/dd/yyyy instead of the UK format I need, which is dd/mm/yyyy.

我尝试将这段代码放在ReportSelectorViewModel的StartDate属性上方,但这无济于事

I've tried putting this code above the StartDate property in the ReportSelectorViewModel, but it doesn't help

[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? StartDate { get; set; }

任何人都可以帮忙吗?

谢谢.

更新

我的HTML呈现的StartDate代码

My Html rendered code for the StartDate

<div class="form-group">
<input class="datepicker" data-val="true" data-val-date="The field StartDate must be a date." data-val-required="Please select a Start Date" id="StartDate" name="StartDate" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="StartDate" data-valmsg-replace="true"></span>
</div>

推荐答案

伙计们

这解决了我的问题,将Html.TextBoxFor声明更改为此

This solved my problem, changing the Html.TextBoxFor declaration to this

@Html.TextBoxFor(model => model.StartDate, new { Value = Model.StartDate.HasValue ? Model.StartDate.Value.ToString("dd/MM/yyyy") : string.Empty, @class = "datepicker" })

这篇关于返回的DatePicker值格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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