Home » Jquery » jquery – Dates are not converted in Unix Timestamp from C#-Exceptionshub

jquery – Dates are not converted in Unix Timestamp from C#-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a method in which I call a method that performs a query returning an IEnumerable of objects, than I perform a group by Linq on this IEnumerable, and elaborate the result of the grouping to insert it in a JQGrid. The problem I’m having is that when the JQgrid is passed to the js file, the dates are not in the format /Date(milliseconds)/, but are formatted as a string, and I cannot localize them in the grid.
This is the call to method that performs the query, returning the IEnumerable object:

IEnumerable documentList = ObjBase.getAll(string.IsNullOrEmpty(columnList) ? null : columnList.Split(';'), sidx, sord, sqlWhere, out totalRows, userid: userId, siteId: siteId);

This is the Group By Linq operation on these IEnumerable:

    var documentContact = documentList.GroupBy(c => new
    {
        c.CODE_CUSTOMERID,
        c.DESC_CONTACT_NUMBER,
        c.CODE_CUSTOMERTYPEID,
        c.CODE_CONTRACT_ID, 
        c.DESC_CONTRACT_NUMBER,
        c.DESC_CONTRACT_STATES,
    })
    .Select(gcs => new DocumentContact()
    {
        CODE_CUSTOMER_ID = gcs.Key.CODE_CUSTOMERID,
        DESC_CONTACT_NUMBER = gcs.Key.DESC_CONTACT_NUMBER,
        CODE_CUSTOMERTYPEID = gcs.Key.CODE_CUSTOMERTYPEID,
        CODE_CONTRACT_ID = gcs.Key.CODE_CONTRACT_ID,
        DESC_CONTRACT_NUMBER = gcs.Key.DESC_CONTRACT_NUMBER,
        DESC_CONTRACT_STATES = gcs.Key.DESC_CONTRACT_STATES,
        CODE_SITEID = siteId,
        Documents = gcs.ToList(),
    });

This is the class of objects that I have after the group by Linq:

public class DocumentContact : ObjBase
{
    public long? CODE_CUSTOMER_ID { get; set; }
    public long? CODE_CUSTOMERTYPEID { get; set; }
    public long? CODE_CONTRACT_ID { get; set; }
    public long CODE_SITEID { get; set; }
    public string DESC_CONTACT_NUMBER { get; set; }
    public string DESC_CONTRACT_NUMBER { get; set; }
    public string DESC_CONTRACT_STATES { get; set; }
    public List<DocumentModel> Documents { get; set; }
}

This is the DocumentModel class:

public class DocumentModel : ObjBase
{
    private long _id;
    private string _documentModel;
    private long? _documentModelTypeId;
    private long? _documentModelIssuingEntityId;
    private string _documentModelFilename;
    private string _documentModelPath;
    private DateTime? _documentModelStart;
    private DateTime? _documentModelEnd;
    private string _documentModelVersion;
    private long _contractId;
    private string _contractNumber;
    private long _contractTypeId;
    private long _contractStateId;
    private string _contractState;
    private long _documentModelStateId;
    private string _documentModelValoId;
    private DateTime? _documentModelUploadDate;
    private byte[] _documentModelContent;
    private long _documentModelFilter;
    private bool _flagDeleted;
    private long _userId;
    private int _orderDocumentModel = 0;
    private string _placeOfIssue;
    private string _issuingEntity;
    private DateTime? _releaseDate;
    private int? _idOpInsert;
    private int? _idOpId;
    private long? _customerId;
    private long? _customerTypeId;
    private string _customerNumber;
    private DateTime? _documentModelInvalidated;
    private bool _flagValidating;
    private string _fileRemotePath;
    private long? _folderId;
    private string _folderName;
    protected new HELPSI_Database HELPSI_Database = Common.HELPSI_Database.HELPSI_Anag;
}

What am I missing, or what can I do to localize correctly the dates, considering te the application is multilingual and the date formats are different for the various languages? Thanks

How to&Answer:

This is the jqGrid setup:

function initializeGridDocument(div_content, gridSelector, div_pager, div_empty) { var gridId = gridSelector; var pager_top = gridSelector + '_toppager'; var vl = loadCookieGroup(); jQuery(gridSelector).jqGrid("GridUnload"); jQuery(gridSelector).jqGrid({ loadui: "block", mtype: 'POST', ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, ajaxEditOptions: { contentType: 'application/json; charset=utf-8', datatype: "json" }, ajaxRowOptions: { contentType: 'application/json; charset=utf-8' }, datatype: "json", serializeGridData: function (postData) { if (postData.searchField === undefined) postData.searchField = null; if (postData.searchString === undefined) postData.searchString = null; if (postData.searchOper === undefined) postData.searchOper = null; if (postData.columnList === undefined) { postData.columnList = $.map($(gridSelector).jqGrid('getGridParam', 'colModel'), function (value, index) { if (value.fetched) return value.index; }).join(';'); } if ((postData.searchField != undefined) && (postData.searchField != "")) { if (postData.searchField.indexOf('DATE_') > -1) { if (postData.searchString.match(/Z$/) == null) postData.searchString = mLoc2UTC(postData.searchString, 'L').toISOString(); } } postData = $.extend(postData, { _search: true, userId: $('#hfCODE_USERID').val(), siteId: $('#ddl_site_Options').val(), stateId: $('#ddl_documentmodelState').val(), folderId: $('#chnfolder').val() }); return JSON.stringify(postData); }, url: 'GestioneDocument.aspx/getDocumentList', editurl: '', colNames: ['', '', '', '', '', '', t('label.customerprovider.contactnumber'), t('label.contractnumber'), t('label.contractsate'), t('label.actions')],//, t('label.documentid'), t('label.type'), t('label.documentstate'), '', t('label.folder'), t('label.filename'), t('label.uploaddate'), t('label.customerprovider.datedocumentmodelstart'), t('label.customerprovider.datedocumentmodelend'), '', t('label.actions')], colModel: [ { name: 'CODE_CUSTOMER_ID', index: 'CODE_CUSTOMERID', jsonmap: 'OGGETTO.CODE_CUSTOMER_ID', sortable: false, hidden: true }, { name: 'CODE_SITEID', index: 'CODE_SITEID', jsonmap: 'OGGETTO.CODE_SITEID', sortable: false, hidden: true }, { name: 'CODE_CUSTOMERTYPEID', index: 'CODE_CUSTOMERTYPEID', jsonmap: 'OGGETTO.CODE_CUSTOMERTYPEID', sortable: false, hidden: true }, { name: 'CODE_CONTRACT_ID', index: 'CODE_CONTRACT_ID', jsonmap: 'OGGETTO.CODE_CONTRACT_ID', sortable: false, hidden: true }, { name: 'imgInfoRequester', label: '', jsonmap: 'OGGETTO.CODE_CUSTOMER_ID', width: 18, fixed: true, align: 'center', search: false, sortable: false, formatter: function (cellvalue, options, rowObject) { if (rowObject.OGGETTO.CODE_CUSTOMER_ID) { switch (rowObject.OGGETTO.CODE_CUSTOMERTYPEID) { case 1: return FormatterAccountGridSales(rowObject.OGGETTO.CODE_CUSTOMER_ID, options, rowObject); break; case 2: return FormatterContactGridSales(rowObject.OGGETTO.CODE_CUSTOMER_ID, options, rowObject); break; } } } }, { name: 'imgInfor', label: '', jsonmap: 'OGGETTO.CODE_CONTRACT_ID', width: 18, fixed: true, align: 'center', search: false, sortable: false, formatter: function (cellvalue, options, rowObject) { if (rowObject.OGGETTO.CODE_CONTRACT_ID) return FormatterContractInfos(cellvalue, options, rowObject); return ""; } }, { name: 'DESC_CONTACT_NUMBER', label: t('label.customerprovider.contactnumber'), index: 'DESC_CONTACT_NUMBER', jsonmap: 'OGGETTO.DESC_CONTACT_NUMBER', sortable: true, width: 125, align: 'left', search: true, group: true, searchoptions: { sopt: ['eq', 'ne', 'cn'] } }, { name: 'DESC_CONTRACT_NUMBER', label: t('label.contractnumber'), index: 'DESC_CONTRACT_NUMBER', jsonmap: 'OGGETTO.DESC_CONTRACT_NUMBER', sortable: true, width: 125, align: 'left', search: true, group: true, searchoptions: { sopt: ['eq', 'ne', 'cn'] } }, { name: 'DESC_CONTRACT_STATES', label: t('label.contractsate'), index: 'DESC_CONTRACT_STATES', jsonmap: 'OGGETTO.DESC_CONTRACT_STATES', sortable: true, width: 125, align: 'left', search: false, group: true, formatter: 'formatTranslate' }, { name: 'CODE_CUSTOMER_ID', jsonmap: 'OGGETTO.CODE_CUSTOMER_ID', formatter: function (cellvalue, options, rowObject) { $.extend(options, { idGridReload: gridSelector, }); $.extend(rowObject.OGGETTO, { CODE_SITEID: $('#ddl_site_Options').val(), }); return FormatterVerificationAction(cellvalue, options, rowObject); }, sortable: false, search: false, width: 450 } ], pager: $(div_pager), //sortname: "CODE_DOCUMENT_MODEL_ID", sortname: "DESC_CONTACT_NUMBER", sortable: true, sortorder: 'desc', shrinkToFit: true, rowNum: 25, rowList: [25, 50, 100, 200, 500], height: 500, viewrecords: true, caption: t('label.documents'), imgpath: '../JS/jqGrid/themes/basic/images', autowidth: true, forceFit: true, grouping: false, groupingView: false, multiselect: false, toppager: true, grouping: vl != 'clear' && vl != '', groupingView: { groupField: [vl], groupText: ['<b>{0}</b>&nbsp;<font color=\'blue\'>({1})</font>'], groupCollapse: false, groupOrder: ['asc'], groupDataSorted: true }, footerrow: false, userDataOnFooter: false, autoencode: false, subGrid: hasPrivilegeWithoutAlerting('showdocumentincontract', $('#hfUSER_PRIVILEGES').val()), recreateFilter: true, jsonReader: { root: function (obj) { if (obj.d != undefined) obj = obj.d; var d = null; if (typeof (obj) == "object") d = obj; else d = eval("(" + obj + ")"); return d.rows; }, repeatitems: false, page: function (obj) { if (obj.d != undefined) obj = obj.d; var d = null; if (typeof (obj) == "object") d = obj; else d = eval("(" + obj + ")"); return d.page; }, total: function (obj) { if (obj.d != undefined) obj = obj.d; var d = null; if (typeof (obj) == "object") d = obj; else d = eval("(" + obj + ")"); return d.total; }, records: function (obj) { if (obj.d != undefined) obj = obj.d; var d = null; if (typeof (obj) == "object") d = obj; else d = eval("(" + obj + ")"); return d.records; }, cell: function (obj) { if (obj.d != undefined) obj = obj.d; var d = null; if (typeof (obj) == "object") d = obj; else d = eval("(" + obj + ")"); return d.OGGETTO; } //'OBJ' }, ondblClickRow: function (rowid, iRow, iCol, e) { }, beforeRequest: function () { var gridid = $(this).attr('id'); $('#' + gridid + '_toppager_center').remove(); $('#' + gridid + '_toppager_right').remove(); return true; }, gridComplete: function () { var grid = $(this), recs = grid.jqGrid('getGridParam', 'records'); if (recs > 0) { $('#gbox_' + gridId).show(); $(div_empty).hide(); } else { $('#gbox_' + gridId).hide(); $(div_empty).show(); } resizeGrid(gridSelector); $(gridSelector + '_toppager_center').remove(); $(gridSelector + '_toppager_right').remove(); //NA: Chiudo il pannello della ricerca quando effettuo il Refresh della griglia jQuery('.ui-icon-refresh').click(function () { var searchIsOpen = $('#fbox_' + gridId).is(':visible'); if (searchIsOpen) { $('#fbox_' + gridId + ' .ui-closer').click(); } }); }, afterInsertRow: function (rowid, rowdata, rowelem) { var existDocument = rowelem.OGGETTO.Documents && rowelem.OGGETTO.Documents.length > 0 ? true : false; var contractUserData = { documentModelList: rowelem.OGGETTO.Documents }; var userData = $(this).jqGrid('getGridParam', 'userData'); if (!userData) userData = []; userData[rowid] = contractUserData; $(this).jqGrid('setGridParam', 'userData', userData); if (!existDocument) { $('#' + rowid).children("td.sgcollapsed").unbind().html("").removeClass("ui-sgcollapsed sgcollapsed"); } }, subGridRowExpanded: function (subgridId, rowid) { var dati = $(this).jqGrid('getGridParam', 'userData'); if (dati && dati[rowid]) { recursiveDateConvert(dati, { onlyDateSet: ['DATE_DOCUMENT_MODEL_START', 'DATE_DOCUMENT_MODEL_END'] }); var subgridTableId = subgridId + "_t"; $("#" + subgridId).html("<table id='" + subgridTableId + "'></table>"); $("#" + subgridTableId).jqGrid({ datatype: "local", autowidth: true, shrinktofit: true, data: dati[rowid].documentModelList, colNames: ['', '', '', t('label.documentid'), t('label.type'), t('label.contractsate'), '', t('label.folder'), t('label.filename'), t('label.uploaddate'), t('label.customerprovider.datedocumentmodelstart'), t('label.customerprovider.datedocumentmodelend'), t('label.actions'), t('label.validate')], colModel: [{ name: 'CODE_DOCUMENT_MODEL_ID', index: 'CODE_DOCUMENT_MODEL_ID', //jsonmap: 'CODE_DOCUMENT_MODEL_ID', sortable: false, hidden: true }, { name: 'CODE_DOCUMENT_MODEL_STATE_ID', index: 'CODE_DOCUMENT_MODEL_STATE_ID', // jsonmap: 'CODE_DOCUMENT_MODEL_STATE_ID', sortable: false, hidden: true }, { name: 'CODE_DOCUMENT_MODEL_ID', index: 'CODE_DOCUMENT_MODEL_ID', //jsonmap: 'CODE_DOCUMENT_MODEL_ID', sortable: false, formatter: function (cellvalue, options, rowObject) { return FormatterDocumentStateImg(cellvalue, options, rowObject); }, width: 15, fixed: true }, { name: 'VALO_DOCUMENT_MODEL_ID', index: 'VALO_DOCUMENT_MODEL_ID', //jsonmap: 'VALO_DOCUMENT_MODEL_ID', sortable: true, group: true, label: t('label.documentid'), search: true, searchoptions: { sopt: ['eq', 'ne', 'cn'] } }, { name: 'documentModelType.DESC_DOCUMENT_MODEL_TYPE', index: 'DESC_DOCUMENT_MODEL_TYPE', //jsonmap: 'documentModelType.DESC_DOCUMENT_MODEL_TYPE', sortable: true, formatter: 'formatTranslate', search: false, group: true, label: t('label.type') }, { name: 'documentModelState.DESC_DOCUMENT_MODEL_STATE', index: 'DESC_DOCUMENT_MODEL_STATE', //jsonmap: 'documentModelState.DESC_DOCUMENT_MODEL_STATE', label: t('label.documentstate'), formatter: 'formatTranslate', search: false, stype: 'select', searchoptions: { sopt: ['eq'], value: "-1:All;1:Active;0:Inactive", }, sortable: true, group: true }, { name: 'CODE_DOCUMENT_MODEL_FOLDER_ID', index: 'CODE_DOCUMENT_MODEL_FOLDER_ID', //jsonmap: 'CODE_DOCUMENT_MODEL_FOLDER_ID', sortable: false, hidden: true }, { name: 'DocumentModelFolderName', index: 'DocumentModelFolderName', //jsonmap: 'DocumentModelFolderName', label: t('label.folder'), formatter: function (cellvalue, options, rowObject) { $.extend(options, { idGridReload: gridSelector, }); $.extend(rowObject.OGGETTO, { CODE_SITEID: $('#ddl_site_Options').val(), }); if (hasPrivilegeWithoutAlerting('updatefolderdocument', $('#hfUSER_PRIVILEGES').val())) { return FormatterDocumentFolder(cellvalue, options, rowObject); } else return (cellvalue !== "") ? cellvalue : t('label.notassignedto'); }, search: false, stype: 'select', searchoptions: { sopt: ['eq'], value: "-1:All;1:Active;0:Inactive", }, sortable: true, group: true }, { name: 'DESC_DOCUMENT_MODEL_FILENAME', index: 'DESC_DOCUMENT_MODEL_FILENAME', //jsonmap: 'DESC_DOCUMENT_MODEL_FILENAME', sortable: true, hidden: true, search: false }, { name: 'DATE_DOCUMENT_MODEL_UPLOAD', index: 'DATE_DOCUMENT_MODEL_UPLOAD', //jsonmap: 'DATE_DOCUMENT_MODEL_UPLOAD', formatter: function (cellvalue, options, rowObject) { if (cellvalue != null) return dateUTCToUserTimeZoneLocale(cellvalue); else return ''; }, sortable: true, search: false, stype: 'text', searchoptions: { sopt: ['lt', 'le', 'gt', 'ge'], dataInit: function (elem) { $(elem).datepicker({ duration: '', constrainInput: true, changeYear: true, changeMonth: true }); } }, width: 200 }, { name: 'DATE_DOCUMENT_MODEL_START', index: 'DATE_DOCUMENT_MODEL_START', //jsonmap: 'DATE_DOCUMENT_MODEL_START', formatter: function (cellvalue, options, rowObject) { if (cellvalue != null) return moment.utc(dotnetDateToJSDate(cellvalue)).format('L'); else return ''; }, sortable: true, search: false, width: 200 }, { name: 'DATE_DOCUMENT_MODEL_END', index: 'DATE_DOCUMENT_MODEL_END', //jsonmap: 'DATE_DOCUMENT_MODEL_END', formatter: function (cellvalue, options, rowObject) { if (cellvalue != null) return moment.utc(dotnetDateToJSDate(cellvalue)).format('L'); else return ''; }, sortable: true, search: false, width: 200 }, { name: 'CODE_DOCUMENT_MODEL_ID', //jsonmap: 'CODE_DOCUMENT_MODEL_ID', formatter: function (cellvalue, options, rowObject) { $.extend(options, { idGridReload: gridSelector, }); $.extend(rowObject.OGGETTO, { CODE_SITEID: $('#ddl_site_Options').val(), }); return FormatterDocumentAction(cellvalue, options, rowObject); }, sortable: false, search: false, width: 450 }, { name: 'FLAG_VALIDATING', index: 'FLAG_VALIDATING', //jsonmap: 'FLAG_VALIDATING', sortable: true, hidden: true }], height: "100%", rowNum: 11, sortname: "name", idPrefix: "s_" + rowid + "_", beforeSelectRow: function (rowID, e) { return false; } }); } } }); jQuery(gridSelector).jqGrid('navGrid', div_pager, { edit: false, add: false, del: false, search: true, refresh: true }, {}, {}, {}, { recreateFilter: true }, {}, {}); jQuery(gridSelector).jqGrid('navGrid', pager_top, { edit: false, add: false, del: false, search: true }, {//search recreateFilter: true, beforeShowSearch: function(form) { $('.ui-reset', form).click(function() { //NA: Effettuo il Reset della ricerca azzerando i campi var gridSearchIsOpen = $('#fbox_gridDocument').is(':visible'); if (gridSearchIsOpen) { $('#fbox_gridCocument .ui-closer').click(); $('#search_gridDocument_top').click(); } }); } }); $(gridSelector).jqGrid('navButtonAdd', gridSelector + '_toppager', { caption: t('label.documentstate'), buttonicon: "ui-icon-document", title: t('label.documentstate'), cursor: 'pointer', onClickButton: function () { $('#dialogDocumentModelStates').dialog({ bgiframe: true, autoOpen: true, width: 800, height: 400, modal: true, resizable: false, position: "top", zIndex: 3000, open: function () { loadGridDMS(); updateElementI18N('dialogDocumentModelStates'); }, close: function () { }, buttons: { 'button.close': function () { $(this).dialog('close'); } } }); } }); 

}