Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's an issue with the way dates are handled. The <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx" rel="nofollow">JavaScriptSerializer</a> uses the following format when dealing with dates: <code>/Date(1302296382818)/</code> which unfortunately makes little sense to jQuery when parsing the GET JSON response so you don't get a real date on the client side but string. So you need a shameless hack in order to convert this string to a real date:</p> <pre><code>myData.Start = new Date(parseInt(data.Start.substr(6))); </code></pre> <p>And here's the full story of the hack:</p> <p>Model:</p> <pre><code>public class Product { public int Id { get; set; } public DateTime Start { get; set; } } </code></pre> <p>Controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Data() { var product = new Product { Id = 1, Start = DateTime.Now, }; return Json(product, JsonRequestBehavior.AllowGet); } [HttpPost] public ActionResult Data(Product product) { return Json(product); } } </code></pre> <p>View:</p> <pre><code>&lt;script type="text/javascript"&gt; var myData; function getData(event) { $.getJSON('/home/data', function (data) { myData = data; myData.Start = new Date(parseInt(data.Start.substr(6))); }); return false; } function changeData(event) { var postData = JSON.stringify(myData); $.ajax({ url: '/home/data', type: 'POST', data: postData, dataType: 'json', contentType: 'application/json; charset=utf-8', success: function (result) { console.log(result); } }); return false; } $(function () { $('#get').click(getData); $('#post').click(changeData); }); &lt;/script&gt; &lt;button id="get"&gt;get&lt;/button&gt; &lt;button id="post"&gt;post&lt;/button&gt; </code></pre> <p>Leaving the <code>TimeSpan</code> case as an exercise to the reader as it will require another shameless hack and I am tired of hacks :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload