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

MVC 5 - Accepting Date Field Across All Browsers

本文关键字:日期 字段 浏览器 MVC      更新时间:2024-01-02

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

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

根据我目前的代码和下面列出的设置,我可以接受Internet Explorer和Chrome、Edge和Safari中的日期返回"购买日期:必须是日期"

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

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

My Field在其模型中声明如下。

    [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所需的日期格式工作。