MCV BeginForm()-DateTime和jQuery DatePicker

MCV BeginForm()-DateTime和jQuery DatePicker,jquery,asp.net-mvc,datetime,datepicker,Jquery,Asp.net Mvc,Datetime,Datepicker,我试图在MVC应用程序中使用jQuery datepicker,但每次提交表单时,都会出现一些问题;可能是日期转换错误,或者只是null 这很难解释,所以我宁愿用代码显示 控制器功能 [HttpPost] public ActionResult GetData(ReportViewModel model) { using (var eh = new ExcelHelper()) { var data = new List<ReportDataModel>

我试图在MVC应用程序中使用jQuery datepicker,但每次提交表单时,都会出现一些问题;可能是日期转换错误,或者只是
null

这很难解释,所以我宁愿用代码显示

控制器功能

[HttpPost]
public ActionResult GetData(ReportViewModel model)
{
    using (var eh = new ExcelHelper())
    {
        var data = new List<ReportDataModel>(); 
        if (!model.SpecificTimeline) { //Do something! }
        else if (model.SpecificTimeline) { //Do something else! }

        return View("Index", model);
    }
}
标记(HTML)

然后我按下提交按钮“生成报告”,我想点击函数
GetData(ReportViewModel model)
,并用正确的数据生成参数
model
。这不会发生,因为
EndDate
属性是
01-01-0001
,即使我从日期选择器中选择了日期

编辑

@using (Html.BeginScripts())
{

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/themes/base/jqueryui-datepicker")

    <script>
        $(function () {
            var startDate = new Date('@Model.StartDate');
            var endDate = new Date('@Model.EndDate');

            $('.start-date').datepicker({
                dateFormat: 'mm-dd-yy'
            }).datepicker('setDate', startDate);

            $('.end-date').datepicker({
                dateFormat: 'mm-dd-yy'
            }).datepicker('setDate', endDate);
        });
    </script>
}
我注意到,如果我没有在datepicker输入中选择任何内容,
DateTime
属性等于
01-01-0001
。如果我在这两个字段中都选择了一个新的日期,则在点击函数
Getdata()
时生成的模型中的日期是正确的

因此,如果我不触摸输入,日期将设置为默认值
01-01-0001


有什么解决方法吗?

因为您只传递
日期
请尝试将
模型属性的类型指定为
[DataType(DataType.date)]
而不是
[DataType(DataType.DateTime)]

读一些


也不一定,但请参考关于使用
EditorFor
而不是
TextBoxFor
的答案,因为您只传递
日期
请尝试将
模型属性的类型指定为
[DataType(DataType.date)]
而不是
[DataType(DataType.DateTime)]

读一些


也不一定,但请参考关于使用
EditorFor
而不是
TextBoxFor

的答案,我不知道是哪一个做了这个把戏,但我将类型改为
[DataType(DataType.Date)]
,而不是旧的,并且还尝试了跟随您提供的链接。有些东西起了作用,因为它现在似乎正在发挥作用。我只有一个问题;如何在第一次创建视图时正确设置日期格式?您可以在
razor
模板本身中指定日期格式,例如
@model.date.ToString(“dd MMM yyyy”)
,方法是在显示
视图时转换为
字符串
,或添加
[DisplayFormat]
模型属性的注释为
[DisplayFormat(DataFormatString=“{0:dd-MMM-yyyy}”)]公共日期时间日期{get;set}
-这些似乎都没有帮助。如果我在输入标记中尝试
@model.Date.ToString(“dd/mm/yyyy”)
,我会得到一个错误。使用
[DisplayFormat(DataFormatString(“dd/mm/yyyyy”)]
什么都不做。这只是为了
显示
不在
编辑器中的视图中,我不知道是哪一个做了手脚,但我将类型改为
[DataType(DataType.Date)]
,并尝试跟随您提供的链接。有些东西起了作用,因为它现在似乎正在发挥作用。我只有一个问题;如何在第一次创建视图时正确设置日期格式?您可以在
razor
模板本身中指定日期格式,例如
@model.date.ToString(“dd MMM yyyy”)
,方法是在显示
视图时转换为
字符串
,或添加
[DisplayFormat]
模型属性的注释为
[DisplayFormat(DataFormatString=“{0:dd-MMM-yyyy}”)]公共日期时间日期{get;set}
-这些似乎都没有帮助。如果我在输入标记中尝试
@model.Date.ToString(“dd/mm/yyyy”)
,我会得到一个错误。使用
[DisplayFormat(DataFormatString(“dd/mm/yyyyy”)]
不起任何作用。这只是为了
显示
而不是在
编辑器中
<div>
    @using (Html.BeginForm("GetData", "Report", FormMethod.Post))
    {
    <div class="form-horizontal">

        <div class="form-group">
            @Html.LabelFor(model => model.SpecificTimeline, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.SpecificTimeline, new { @class = "" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StartDate, new { @class = "control-label col-md-2 " })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.StartDate, new { @class = "form-control datepicker" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EndDate, new { @class = "control-label col-md-2 " })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.EndDate, new { @class = "form-control datepicker" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Generate Report" class="btn btn-primary" />
            </div>
        </div>
    </div>
    }
</div>
@using (Html.BeginScripts())
{

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/themes/base/jqueryui-datepicker")

    <script>
        $(function () {
            var startDate = new Date('@Model.StartDate');
            var endDate = new Date('@Model.EndDate');

            $('.start-date').datepicker({
                dateFormat: 'mm-dd-yy'
            }).datepicker('setDate', startDate);

            $('.end-date').datepicker({
                dateFormat: 'mm-dd-yy'
            }).datepicker('setDate', endDate);
        });
    </script>
}
public class ReportViewModel
{
    public ReportViewModel() { }

    public ReportViewModel(bool specificTimeline, DateTime startDate, DateTime endDate, List<ReportDataModel> data)
    {
        SpecificTimeline = specificTimeline;
        StartDate = startDate;
        EndDate = endDate;
        Data = data;
    }

    public bool SpecificTimeline { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime StartDate { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime EndDate { get; set; }

    public List<ReportDataModel> Data { get; set; }
}
startDate: 17-02-2016
endDate: 22-02-2016

format: dd-mm-yy