MVC ajax 表单提交 returns DateTime 的默认日期 属性

MVC ajax form submission returns default date of DateTime property

我在 select 和 post 日期范围内使用下面的 Ajax 表格。

查看模型:

public class ViewFormSubmissionsVM
    {
        public string FormName { get; set; }
        public Guid FormId { get; set; }

        [Display(Name="From:")]
        public DateTime From { get; set; }

        [Display(Name = "To:")]
        public DateTime To { get; set; }
    }

Razor/HTML:

@model ViewFormSubmissionsVM

    @using (Ajax.BeginForm("ViewFormSubmissions", "Form", new AjaxOptions { HttpMethod = "POST", OnSuccess = "onGetSubmissionSuccess", OnFailure = "onGetSubmissionError" }, new { @class = "form-horizontal" }))
    {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(x => x.FormId)
        <div class="row col-md-12">
            <div class="pull-right">
                <label class="control-label col-md-3">@Html.DisplayNameFor(x => x.From)</label>
                @Html.TextBoxFor(x => x.From, "{0:MM/dd/yyyy}", new { @class = "form-control col-md-3", @data_picker = "date-picker" })
                <label class="col-md-3">@Html.DisplayNameFor(x => x.To)</label>
                @Html.TextBoxFor(x => x.To, "{0:MM/dd/yyyy}", new { @class = "form-control col-md-3", @data_picker = "date-picker" })
                <input type="submit" class="btn btn-primary" value="Search" />
            </div>
        </div>
    }

JQuery 日期选择器:

$(document).ready(function () {
    $('*[data-picker="date-picker"]').datepicker();
});

这个问题是在提交表单时,"From" 和 "To" 日期时间属性收到默认日期值而不是 selected。

我是不是漏掉了什么重要的东西!!我已经以不同的形式使用过这些代码,但以前从未体验过。

谁能帮我摆脱这个。

非常感谢。

首先,我认为你应该调用这个重载:

using (Ajax.BeginForm("ViewFormSubmissions", "Form", null, new AjaxOptions{ 
    HttpMethod = "POST", 
    OnSuccess = "onGetSubmissionSuccess", 
    OnFailure = "onGetSubmissionError" 
    }, 
    new {@class = "form-horizontal" }))

第三个参数为 null。

其次,检查您的日期时间格式。您可以在此处找到更多信息:MVC DateTime binding with incorrect date format, ASP.NET MVC datetime culture issue when passing value back to controller or http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization.