I am working on an MVC application with an Angular front end. Before I was manually passing the view-model which contains a number of different fields including date-times directly to the view as an ActionResult parameter, then manually serializing it in the view with Newtonsoft before binding it to the angular model and displaying it.
ie:
in the controller:
ActionResult MainView()
{
....
return View(model);
}
in the view:
ng-data-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model.ItemList))"
This worked fine and all the information was displayed properly.
However I recently switched to using a JsonResult function and an Angular $http call to automatically update the data in the view in the background. Everything is working fine except that the DateTime fields aren't being properly translated/formatted by whichever serializer the JsonResult object uses internally.
ie:
in the controller:
public JsonResult REIDataRefresh()
{
...
return Json(model.ItemList, JsonRequestBehavior.AllowGet);
}
in the angular:
$scope.data
$scope.LoadData = function() {
$http({
method: "GET",
url: 'Home/REIDataRefresh',
params:{'_': +new Date() }
}).then(function success(response) {
$scope.data = response.data;
}, function error(errResponse) {
alert("data " + errResponse.data + " status " + errResponse.status + " headers " + errResponse.headers + "config " + errResponse.config + " statusText " + errResponse + " xhrStat " + errResponse.xhrStatus);
});
};
// Initial data load
$scope.LoadData();
When I run the model through Newtonsoft.JsonConvert.SerializeObject
it all formats correctly and the dates are exactly what is expected -
mm/dd/yyy
But when I pass them through the Json()
return via the JsonResult
action method the DateTimes looks like this:
Most of these MVC constructs are new to me so my only guess is that the serializer that is baked into Json()
works differently than the one I was using before. So there are only two potential solutions I can think of: either find out how to tell angular how to format those weird DateTimes it's getting from the JsonResult, or tell Json to use the serializer I was using before instead of its default one, but I have no idea how to accomplish either of these things or if they're even the correct solution.