MVC 5 - 在所有浏览器中接受日期字段

MVC 5 - Accepting Date Field Across All Browsers

你好,我目前在接受所有 4 种浏览器、Internet Explorer、Chrome、MVC 5 中的 Edge 和 Safari 的日期时遇到了真正的痛苦。

我正在使用 Jquery-ui 日期选择器。

使用我当前的代码和下面概述的设置,我可以在 Internet Explorer 和 Chrome、Edge 和 Safari return "The field Date of Purchase: must be a date."

中接受日期

我的技能很基础,但在过去的一年里我学到了很多,但老实说,我更沮丧的是像数据字段这样简单的东西会如此痛苦,搜索已经只是 运行 我转了一圈,我有工作代码,但不是所有浏览器都可以工作。

如果有人能给我指出正确的方向来解决类似的问题(我找了一整天后找不到),或者一个涵盖所有基础的解决方案,或者只是帮助照亮我的昏暗它将不胜感激。

我的字段在它的模型中是这样声明的。

    [Required]
    [Display(Name = "Date of Purchase:")]
    [DisplayFormat(DataFormatString = "{0:yyyy/MMM/dd}",
    ApplyFormatInEditMode = true)]
    public System.DateTime date_of_purchase { get; set; }

我的web.config声明全球化如下:

<globalization culture="auto" uiCulture="auto" />

在我的 HTML Razor 页面上,我调用 Jquery Datepicker,如下所示:

<script>
$(document).ready(function () {
    $("#@Html.IdFor(m => m.date_of_purchase)").datepicker({dateFormat: 'yy/MM/dd',  maxDate: "0" });
});
</script>

日期条目的表单字段如下:

<div class="form-group">
    @Html.LabelFor(model => model.date_of_purchase, htmlAttributes: new { @class = "control-label col-md-4" })
        <div class="col-md-8">
            @Html.EditorFor(model => model.date_of_purchase, new { htmlAttributes = new { @class = "form-control", @readonly = "true", @placeholder = "Required" } })
            @Html.ValidationMessageFor(model => model.date_of_purchase, "", new { @class = "text-danger" })
        </div>
</div>

我的控制器在下方,但我认为这与问题沼泽标准无关。

    public async Task<ActionResult> VehicleRegistration([Bind(Include = "User_Email,Serial_No,IMEI_No,dealer_id,dealer_name,date_of_purchase")] VehicleRegistration vehicleReg)
    {
        if (ModelState.IsValid)
        {
            IQueryable<Dealer> dealerSort;
            dealerSort = db.Dealers.Where(o => o.Name == vehicleReg.dealer_name);
            vehicleReg.dealer_id = dealerSort.Single().DealerID;
            IQueryable<AspNetUser> UserSort;
            UserSort = db.AspNetUsers.Where(o => o.Email == vehicleReg.User_Email);
            string userSortResult = UserSort.Single().Id;
            string userTitle = UserSort.Single().Title;
            string userSurname = UserSort.Single().Surname;
            string userTel = UserSort.Single().Home_Phone;
            string userMob = UserSort.Single().Mobile_Phone;

            string callbackUrl = await SendEmailVehicleRegistrationRequest(userSortResult, "Vehicle Registration Request", vehicleReg.User_Email, vehicleReg.Serial_No, vehicleReg.IMEI_No, vehicleReg.dealer_id, vehicleReg.dealer_name, vehicleReg.date_of_purchase, userTitle, userSurname, userTel, userMob);

            ViewBag.Message = "An email has been sent to " + vehicleReg.dealer_name + ", requesting them to complete the sale of your vehicle." +
            " A copy has also been sent to Swift Command Support, if you do not receive confirmation within 3 days please contact Swift Command Support.";
            return View("Info");
        }

        return View(vehicleReg);
    }

这个解决方案感谢@StephenMuecke 发现了我的问题。

如果我修改了我的模型并将月份从 MMM 更改为 MM,下面的新代码。

[Required]
[Display(Name = "Date of Purchase:")]
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}", ApplyFormatInEditMode = true)]
public System.DateTime date_of_purchase { get; set; }

在 Jquery 日期选择器的设置页面上,我将日期格式更改为 'dd/mm/yy',这是我一直想要的,但无法跨两个浏览器工作,别介意所有 5 个。之前是 'yy/MM/dd'.

<script>
    $(document).ready(function () {
    $("#@Html.IdFor(m => m.date_of_purchase)").datepicker({ dateFormat: 'dd/mm/yy',  maxDate: "0" });
});
</script>

感谢大家的意见,我知道它可以在 IE、Edge、Chrome、FireFox 和 Safari 上以我需要的日期格式工作。