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

时间:2016-04-04 10:13:52

标签: javascript asp.net-mvc date razor jquery-ui-datepicker

您好我目前正在接受MVC 5中所有4种浏览器,Internet Explorer,Chrome,Edge和Safari的日期。

我正在使用Jquery-ui Date Picker。

根据我目前的代码和设置,我可以接受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);
    }

1 个答案:

答案 0 :(得分:2)

This Solution is thanks to @StephenMuecke who spotted my problem.

if my Model I amended it and changed the Month from MMM to MM new code below.

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

On the page the set up for Jquery Date Picker I changed the date format to 'dd/mm/yy' which is what I always wanted but could not get to work across two browsers never mind all 5. It was previously 'yy/MM/dd'.

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

Thanks to everyones input I know have it working in the date format I require across IE, Edge, Chrome, FireFox and Safari.