解析错误的日期格式

时间:2016-08-01 07:18:08

标签: asp.net-mvc

我使用bootstraps datepicker来选择日期,并使用下拉列表来选择供应商。选择供应商时,应向我显示供应商的拒绝原因。但问题是,我没有得到dd-mm-yyyy那天,而是在控制器的GetRejectedCauses中得到mm-dd-yyyy。脚本中的startTime和endTime向我显示正确的格式dd-mm-yyyy。因此,在将日期格式解析为控制器时,某些事情会出错!

查看

<div class="form-group">
   <p>@Html.LabelFor(model => model.StartTime)</p>
   <p>@Html.TextBoxFor(model => model.StartTime, "{0:dd-MM-yyyy}", new { id = "selectedStartDate" }) </p>
   <p>@Html.ValidationMessageFor(model => model.StartTime)</p>
</div>

<div class="form-group">
   <p>@Html.LabelFor(model => model.EndTime)</p>
   <p>@Html.TextBoxFor(model => model.EndTime, "{0:dd-MM-yyyy}", new { id = "selectedEndDate" }) </p>
   <p>@Html.ValidationMessageFor(model => model.EndTime)</p>
</div>
<div>
   @Html.DropDownListFor(m => m.SelectedSupplier,    Model.SelectedSupplierList, "Choose a Supplier", new { @class = "form-control" })
</div>

<script type="text/javascript">
    $(function() {
        $("#selectedStartDate")

        .datepicker(
        {
            autoclose: true,
            todayHighlight: true,
            format: "dd-mm-yyyy"
        }

        );
        $("#selectedEndDate")
        .datepicker(
        {
            autoclose: true,
            todayHighlight: true,
            format: "dd-mm-yyyy",
        });
    });
</script>

<script type="text/javascript">
    $(document)
    .ready(function()
    {
        $("#SelectedSupplier")
        .change(function()
        {
            $.ajax(
            {
                type: 'GET',
                url: '@Url.Action("GetRejectedCauses")',            
                dataType: 'json',
                cache: false,
                format: "dd-mm-yyyy",
                data:
                {
                    selectedSupplier :  $("#SelectedSupplier").val(),
                    StartTime :  $("#selectedStartDate").val(),
                    EndTime :  $("#selectedEndDate").val(),
                },
               success: function(data, textStatus)
                {
                    alert(data.SelectedSupplier);
                },
                error: function(xhr, textStatus, errorThrown)
                {
                    alert('Could not show rejected causes.' + xhr);
                }

            });
    });
 });

模型

   [Display(Name = "Date From")]
   [DataType(DataType.Date)]
   [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",  
    ApplyFormatInEditMode = true)]
    public DateTime ?StartTime { get; set; }

    [Display(Name = "Date To")]
    [DataType(DataType.Date)]
    public DateTime? EndTime { get; set; }

控制器

    public JsonResult GetRejectedCauses(Rejected model)
    {
        return Json
            (
            new { success = true,
                SelectedSupplier = model.SelectedSupplier                 
            }, JsonRequestBehavior.AllowGet);
    }

0 个答案:

没有答案