Converting DateTime objects into JSON format for use in AJAX calls

When utilizing my AJAX method, it returns a view model that is serialized as a data structure using

JavaScriptSerializer().Serialize()
. Among this data are several nullable DateTime? properties.

I recently discovered that these dates appear in JavaScript as strings in the format "/Date(1480551007625)/". With some investigation, I found out how to convert this into a JavaScript date.

The dilemma arises when attempting to post back the data to my AJAX methods, resulting in the error message:

/Date(1480551007625)/ is not a valid value for DateTime.

This error persists even when I have made no changes to that value within the view model! Essentially, I am simply posting back the same unaltered view-model object.

How can I convert the DateTime properties within my view mode on the client side so that they may be successfully posted back to the server and converted back to DateTime properties?

Answer №1

In order to properly handle the data, it appears that you may need to execute a command similar to:

serializer.Deserialize<DateTime>("/Date(1480551007625)/");

If you omit the <DateTime>, the Deserialize function is likely to return a string instead of a DateTime object. Converting a string in this specific format to a DateTime object may not yield the desired results.

Answer №2

It was quite unexpected, but it turned out to be a lot more hassle than I had anticipated. To simplify things, I decided to convert the date fields in my view model into strings and store them in the format "2016|12|1".

And just like that, the issue was resolved.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Autocomplete feature failing due to unsuccessful Ajax request

I am currently facing an issue with an ajax request to fetch autocomplete data from my database. Here is the code snippet for the request : $(function() { $("#client").autocomplete({ minLength: 1, autoFocus: true, source: funct ...

Avoid opening the page when attempting to log in with jquery, ajax, and php

I am facing an issue with my code. I have a file named "index.html" which contains a login form. Another file called "dash.js" retrieves the username and password from the login form and redirects to "connectdb.php" to check the login credentials with the ...

Combining two JSON datasets and presenting the merged information

I need help parsing two sets of json data from the following URLs: https://raw.githubusercontent.com/openfootball/football.json/master/2015-16/en.1.json https://raw.githubusercontent.com/openfootball/football.json/master/2016-17/en.1.json My goal is to di ...

The UseEffect hook continues to run even if the dependency (router.query) remains the same

useEffect(() => { console.log('applying filter'); const updatedFilters = { status: { values: { label: router.query.status, value: router.query.status }, }, // Add additional filter properties here... }; ...

Ensure that JSON requests do not contain line breaks within the <div> element

Storing data in a database using json/jquery/ajax has been successful for me. When I retrieve the data in a textarea, it displays exactly as expected. However, loading the data into a DIV results in the absence of line breaks. I have tried various CSS styl ...

Function anomalies triggered by delayed setState updates with React Hooks

Creating a Quiz app with React involves fetching questions as an array and managing the following states: An array containing all question details - statement, options, chosen answer, status (answered, marked for review, unvisited); An object holding info ...

Refresh the angular list filter by clicking on it

I'm struggling with updating an Angular list after the filter is changed. Below is the HTML code I am using: <li ng-repeat="items in list | filter:filterList" style="list-style-type:none"> {{items}} </li> Additionally, here is the o ...

.htaccess causing issues with Ajax when rewriting URLs

After setting up the basic project and configuring htaccess for URL rewriting, I also implemented jQuery AJAX. Options +FollowSymLinks RewriteEngine On RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f Rewri ...

Displaying the unique values based on the key attribute

I'm developing a category filter and struggling to showcase the duplicate options. Within my array of objects: filterData = [ { name: 'Aang', bender: 'yes', nation: 'Air', person: 'yes', show: 'ATLA&apo ...

What is the best way to accurately establish a new name for the evolving scope

var tags_offset=[]; $scope.getRelations = function(id, ref, subRef=0){ tags_offset[ref+'-'+subRef]=0; $http.get( CONS.appHttp+ '/tags.php?ID='+id +'&ref='+ref +'&contentType='+subRe ...

What is the best way to upgrade to a specific version of a child dependency within a module?

npm version: 7.24.2 Looking for assistance on updating a child dependency. The dependency in question is: vue-tel-input This dependency relies on libphonenumber-js with version ^1.9.6 I am aiming to update libphonenumber-js to version ^1.10.12. I have ...

Guide to implementing client-side validation in MVC 4 without relying on the model

Currently, I am developing an ASP.NET MVC 4 project where I have decided not to utilize View Models. Instead, I am opting to work with the classes generated from the Entities for my Models. I am curious if there are alternative methods to achieve this. A ...

[Error]: Unable to access the 'getCroppedCanvas' property as it is undefined in React Cropper

I am currently utilizing the "React Cropper" library (https://www.npmjs.com/package/react-cropper). I have included this code snippet (similar to many examples): import React from 'react'; import Cropper from 'react-cropper'; export ...

What is the best way to retrieve an array of objects from Firebase?

I am looking to retrieve an array of objects containing sources from Firebase, organized by category. The structure of my Firebase data is as follows: view image here Each authenticated user has their own array of sources with security rules for the datab ...

Autocomplete suggestions tailored to specific categories in real-time

Having trouble creating an autocomplete feature with dynamic values based on a combobox using CodeIgniter. I've attempted using AJAX without success. Below is my AJAX code for calling items in a category: <script type="text/javascript"> $(docu ...

Can I safely keep a JWT in localStorage while using ReactJS?

As I work on developing a single page application with ReactJS, one question comes to mind. I came across information indicating that using localStorage may pose security risks due to XSS vulnerabilities. However, given that React escapes all user input, ...

Guide on accessing checkbox id in Vue3 and determining its checked status

<div> <input type="checkbox" class="delete-checkbox" :id=this.products[index].sku @click="setDelete(this.products[index].sku)" /> </div> I'm currently working on a Vuex applicatio ...

Looking for guidance on restructuring a JSON object?

As I prepare to restructure a vast amount of JSON Object data for an upcoming summer class assignment, I am faced with the challenge of converting it into a more suitable format. Unfortunately, the current state of the data does not align with my requireme ...

Having trouble sending data to API with Node, Express, and vanilla JavaScript POST form

I am currently utilizing Node JS along with Express JS in order to implement a form submission that pushes data into the database. Below is my form structure <form action="/pokedex/register/poke_submission" method="POST"> ...

Unveiling the Technique: Adjusting Field Visibility When Dropdown is Altered

I tried to find a solution on Stackoverflow for displaying/hiding a field based on dropdown selection using either jQuery or inline JavaScript. However, I am facing difficulties when implementing this within a table. Let's start with an easy approach ...