0

My records are coming back from the server in C# in this format:

(via network tab / preview on the network call)

DateModified: "/Date(1640035318903)/"

enter image description here

What would cause this issue? Why won't it just come back like this?

enter image description here

This is how I'm returning it via my controller via a post:

        /// <summary>
        /// Gets the application file manager file system items.
        /// </summary>
        /// <param name="fileManagerJsParam">The file manager js parameter.</param>
        /// <returns>
        /// ActionResult
        /// </returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> GetFileManagerFileSystemItems(FileManagerJsParamModel fileManagerJsParam)
        {
            if (fileManagerJsParam == null)
            {
                throw new ArgumentNullException(nameof(fileManagerJsParam));
            }

            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    success = false,
                    errorMessage = string.Join(
                    ",",
                    ModelState.Values.Where(e => e.Errors.Count > 0)
                    .SelectMany(e => e.Errors)
                    .Select(e => e.ErrorMessage)
                    .ToArray())
                });
            }

            var csModel = mapper.Map<FileManagerCsParamModel>(fileManagerJsParam);

            var result = await fileManagerUtilities.GetApplicationFileManagerFileSystemItems(csModel, 107);

            return Json(result.FileSystemItems);
        }

Here is my javascript that calls it:

function getItemsFileManager(e) {
    var deferred = $.Deferred()

    let fullUrl = baseEndPoint + loadAction;

    // check to see if we have different base end point / load action urls
    if (e && e.dataItem) {
        var fileManagerAPIItems = getFileManagerAPIItems(e.dataItem);

        if (fileManagerAPIItems) {
            fullUrl = fileManagerAPIItems.BaseEndPoint + fileManagerAPIItems.LoadAction;
        }
    }

    let fileManagerJsParam = getFileManagerParams(e, true);

    if (!fileManagerJsParam) {
        console.log("fileManagerParms not set in getItemsFileManager @ " + new Date());
        genericErrorMessage();
        return;
    }

    $.post(BuildSafeURL(fullUrl, null), fileManagerJsParam)
    .done(function (data) {
        deferred.resolve(data);
    })
    .fail(function (error) {
        deferred.reject(getItemGenericErrorMessage(e));

        if (error && error.responseJSON && error.responseJSON.errorMessage) {
            console.log("error⚠️", error.responseJSON.errorMessage);
        }
        else {
            console.log("error⚠️", error);
        }
    });

    return deferred.promise();
}
tvb2754
  • 107
  • 8
  • That's Microsoft's JSON representation of a data value. What problem is it causing? – D Stanley Dec 21 '21 at 02:06
  • @DStanley: Well it is binding directly to my control and displays like "/Date(1640035318903)/" instead of an actual date? – tvb2754 Dec 21 '21 at 02:14
  • @DStanley I figured it out. I'm posting my answer. Your information helped me search differently and find the answer. – tvb2754 Dec 21 '21 at 02:41

1 Answers1

2

I found the result from this post: https://stackoverflow.com/a/9302054 (Approach 2).

I had to update all my controller http post calls to return Task<JsonResult> / return new CustomJsonResult { Data = result.FileSystemItems };

Here is the code:

namespace WebPortal.MVC.Utilities
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    /// <summary>
    /// CustomJsonResult
    /// </summary>
    /// <seealso cref="JsonResult" />
    public class CustomJsonResult : JsonResult
    {
        private const string DateFormat = "yyyy-MM-ddTHH:mm:ssZ";

        /// <summary>
        /// Executes the result.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <exception cref="System.ArgumentNullException">context</exception>
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;

            if (!string.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }

            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }

            if (Data != null)
            {
                // Using Json.NET serializer
                var isoConvert = new IsoDateTimeConverter();
                isoConvert.DateTimeFormat = DateFormat;
                response.Write(JsonConvert.SerializeObject(Data, isoConvert));
            }
        }
    }
}

        /// <summary>
        /// Gets the application file manager file system items.
        /// </summary>
        /// <param name="fileManagerJsParam">The file manager js parameter.</param>
        /// <returns>
        /// ActionResult
        /// </returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> GetFileManagerFileSystemItems(FileManagerJsParamModel fileManagerJsParam)
        {
            if (fileManagerJsParam == null)
            {
                throw new ArgumentNullException(nameof(fileManagerJsParam));
            }

            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    success = false,
                    errorMessage = string.Join(
                    ",",
                    ModelState.Values.Where(e => e.Errors.Count > 0)
                    .SelectMany(e => e.Errors)
                    .Select(e => e.ErrorMessage)
                    .ToArray())
                });
            }

            var csModel = mapper.Map<FileManagerCsParamModel>(fileManagerJsParam);

            var result = await fileManagerUtilities.GetApplicationFileManagerFileSystemItems(csModel, 107);

            return new CustomJsonResult { Data = result.FileSystemItems };
        }

Results:

enter image description here

enter image description here

tvb2754
  • 107
  • 8