使用Kendo Grid获取数据时日期格式正在变化

时间:2016-04-28 03:40:33

标签: asp.net-mvc datetime kendo-ui kendo-grid

我正在尝试使用一些参数获取数据并将数据加载到kendo网格。但是当我使用日期参数时,日期格式正在发生变化,因此在服务器端显示错误的日期。

作为我正在使用的参数的示例:新日期(“2016年4月1日”)。但在服务器方面它变成04/01/2016这是错误的。

function passFilterCstDetails() {

        var statemenetInquiryParameter = {};

        statemenetInquiryParameter.isPrintZero = true;
        statemenetInquiryParameter.isPrintPayments = true;
        statemenetInquiryParameter.isPrintAdjust = true;
        statemenetInquiryParameter.cst_stmt_from = new Date("April 01, 2016");
        statemenetInquiryParameter.cst_stmt_to = new Date("April 12, 2016");
        statemenetInquiryParameter.customerCode = 007;

        return {
            statemenetInquiryParameter: statemenetInquiryParameter
        }
    }
<div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                        @(Html.Kendo().Grid<ServicePROWeb.ServiceProWCFService.CstTran>()
                            .Name("gridCustomerCstTranDetails")
                            .Columns(columns =>
                            {
                                columns.Bound(p => p.cst_inv_date).Title("Invoice Date").HtmlAttributes(new { @style = "text-align: right;" }).Format(Session["DisplayFormat_GridDate"].ToString()).Width(80);
                                columns.Bound(p => p.cst_type).Title("Type").Width(80);
                                columns.Bound(p => p.cst_ih_invno).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Invoice Number").Width(80);
                                columns.Bound(p => p.cst_dr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Debit").Width(80);
                                columns.Bound(p => p.cst_cr_amount).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Credit").Width(80);
                                columns.Bound(p => p.cst_dr_balance).Format("{0:n2}").HtmlAttributes(new { @style = "text-align: right;" }).Filterable(false).Title("Balance").Width(80);
                            })
                            .Selectable()
                            .Sortable()
                            .Scrollable()
                            .Resizable(resize => resize.Columns(true))
                            .HtmlAttributes(new { style = "cursor:pointer;height:auto;width:auto;margin-top: 0px;" })
                            .DataSource(dataSource => dataSource
                            .Ajax()
                            .Read(read => read.Action("LoadCustomerStatementEnquiryDetails", "Stage").Data("passFilterCstDetails")))
                        )
                    </div>
                </div>

public class StatemenetInquiryParameter
{
    public decimal customerCode { get; set; }
    public DateTime cst_stmt_from { get; set; }
    public DateTime cst_stmt_to { get; set; }
    public bool isPrintZero { get; set; }
    public bool isPrintPayments { get; set; }
    public bool isPrintAdjust { get; set; }
}

public ActionResult LoadCustomerStatementEnquiryDetails([DataSourceRequest]DataSourceRequest request, StatemenetInquiryParameter statemenetInquiryParameter)
    {
        List<CstTran> l = new List<CstTran>();

        for (int i = 0; i < 12; i++)
        {
            CstTran c = new CstTran();

            c.cst_inv_date = statemenetInquiryParameter.cst_stmt_from.AddDays(i);
            c.cst_type = "I";
            c.cst_ih_invno = i + 1;
            c.cst_dr_amount = i;
            c.cst_cr_amount = 0;
            c.cst_dr_balance = c.cst_dr_balance + i;

            l.Add(c);
        }

        return Json(l.ToDataSourceResult(request));
    }

1 个答案:

答案 0 :(得分:0)

我也有这个问题,我通过在保存时检查数据格式来解决它。

1-保存时查找数据

 var dataS = $("#grid").data("kendoGrid").dataSource;
 var updatedData = dataS._data;

2-检查格式然后保存,我的数据参数是rsrc_dt

    var dateValue = updatedData[i].rsrc_dt;
    var day; var year; var mon;
    if (typeof dateValue === 'string' || dateValue instanceof String) {
    day = dateValue.split('/')[0];  // use when date is not in correct string format
    mon = dateValue.split('/')[1];
    year = dateValue.split('/')[2];
    var dateS = day + '/' + mon + '/' +year;
    serverData[i].rsrc_dt = dateValue;
    }
    else if (dateValue instanceof Date) {
    var date = new Date(updatedData[i].rsrc_dt);
    year = date.getFullYear();
    day = date.getDate();
    day = day < 10 ? '0' + day : day
    mon = date.getMonth();
    mon = mon + 1;
    mon = mon < 10 ? '0' + mon : mon;
    var dateF = day + "/" + mon + "/" + year;
    serverData[i].rsrc_dt = dateF;
    }

3-您也可以尝试这样做,在这样的字段模板中提供数据格式

     { field: "rsrc_dt", title: "Session Date", format: "{0:dd/MM/yyyy}", editor: dateTimeEditor, width: 73, attributes: { "class": "azN" }, },

4-使用DateEditor

  function dateTimeEditor(container, options) {
  $('<input name="editableBlock" data-text-field="' + options.field +    '" data-value-field="' + options.field + '" data-bind="value:' + options.field + '" data-format="' + options.format + '"/>')
            .appendTo(container)
            .kendoDatePicker({ min: btch_strt_dt });

}