How to format date returned from .NET ajax call.

How to format date returned from .NET ajax call.

drettbergdrettberg Posts: 6Questions: 3Answers: 1

Here's my client-side code:

                var url = "@Url.Action("GetDisplayMessagesDataTables", "Admin", new {locId = "__locId__"})";
                var locId = $("#displayselect option:selected")[0].value;
                url = url.replace("__locId__", locId);

                editor = new $.fn.dataTable.Editor( {
                    ajax: { url,
                        data: function (d) {
                            if (d.action == "create")
                                d.data[0].DisplayLocation_ID = $("#displayselect option:selected")[0].value;
                        }
                    },
                    table: "#messagegrid",
                    fields: [{
                        label: "Message Index:",
                        name: "MessageIndex"
                    }, {
                        label: "Message Text:",
                        name: "MessageText",
                    }, {
                        label: "Expiration:",
                        name: "Expiration",
                        type: "datetime",
                        def: "",
                        format: "MM-DD-YYYY hh:mm A",
                        fieldInfo: "mm-dd-yyyy hh:mm am/pm"
                    }],
                    formOptions: {
                        inline: {
                            onBlur: 'submit'
                        }
                    }
                } );

                $('#messagegrid').DataTable({
                    dom: "Bfrtip",
                    destroy: true,
                    ajax: url,
                    columns: [
                        { data: "MessageIndex" },
                        { data: "MessageText" },
                        { data: "Expiration" }
                    ],
                    select: true,
                    buttons: [
                        { extend: "create", editor: editor },
                        { extend: "edit", editor: editor },
                        { extend: "remove", editor: editor }
                    ]
                })
            });

Server-side code:

        public JsonResult GetDisplayMessagesDataTables(int locId) {
            var formData = HttpContext.Request.Form;
            using (var db = new Database("sqlserver", ConfigurationManager.ConnectionStrings["MjnDisplaysConnectionString"].ConnectionString)) {
                Field f = new Field("DisplayMessage_ID").Set(false);

                Field ex = new Field("Expiration")
                    .SetFormatter((obj, data) => {
                        if (obj != null && (string)obj != "") {
                            DateTime dt;
                            if (DateTime.TryParse(obj.ToString(), out dt))
                                return dt;
                            else
                                return null;
                        } else
                            return null;
                    })
                    .GetFormatter((obj, data) => {
                        string ret = null;
                        if (obj != null) {
                            DateTime dt = (DateTime)obj;
                            ret = dt.ToString("MM-dd-yyyy hh:mm tt");
                        }

                        return ret;
                    });

                var response = new Editor(db, "DisplayMessage", "DisplayMessage_ID")
                    .Model<DisplayMessageData>()
                    .Where("DisplayLocation_ID", locId)
                    .Field(f)
                    .Field(ex)
                    .Field(new Field("MessageText").Xss(false))
                    .Process(formData)
                    .Data();

                return Json(response, JsonRequestBehavior.AllowGet);
            }
        }

Expiration is my datetime database field and it always displays as a JSON date (i.e. /Date(1434253400000) in the data table. I've looked through the Date/Time examples but don't see where anything special is done as far as formatting on the client side.

What am I missing?

Thanks,
Dave

This question has an accepted answers - jump to answer

Answers

  • drettbergdrettberg Posts: 6Questions: 3Answers: 1
    Answer ✓

    Found it. Had the field as a DateTime in the model class. Changed that to string and all is well.

    Thanks,
    Dave

  • allanallan Posts: 63,204Questions: 1Answers: 10,415 Site admin

    Thanks for posting back - great to hear you've got it working now.

    The problem with the DateTime in the model is that it can't be directly represented in JSON.

    Allan

This discussion has been closed.