如何告诉MVC中的JsonResult操作方法使用不同的序列化程序方法
我正在开发一个具有角度前端的MVC应用程序。在此之前,我手动将包含许多不同字段(包括日期时间)的视图模型作为ActionResult参数直接传递给视图,然后使用Newtonsoft在视图中手动序列化它,然后将其绑定到角度模型并显示 即: 在控制器中:如何告诉MVC中的JsonResult操作方法使用不同的序列化程序方法,json,asp.net-mvc,angular,serialization,json.net,Json,Asp.net Mvc,Angular,Serialization,Json.net,我正在开发一个具有角度前端的MVC应用程序。在此之前,我手动将包含许多不同字段(包括日期时间)的视图模型作为ActionResult参数直接传递给视图,然后使用Newtonsoft在视图中手动序列化它,然后将其绑定到角度模型并显示 即: 在控制器中: ActionResult MainView() { .... return View(model); } public JsonResult REIDataRefresh() { ...
ActionResult MainView()
{
....
return View(model);
}
public JsonResult REIDataRefresh()
{
...
return Json(model.ItemList, JsonRequestBehavior.AllowGet);
}
他认为:
ng-data-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model.ItemList))"
这工作正常,所有信息都显示正确
然而,我最近转而使用JsonResult函数和$http调用在后台自动更新视图中的数据。除了DateTime字段没有被JsonResult对象内部使用的序列化程序正确翻译/格式化之外,一切都正常
即:
在控制器中:
ActionResult MainView()
{
....
return View(model);
}
public JsonResult REIDataRefresh()
{
...
return Json(model.ItemList, JsonRequestBehavior.AllowGet);
}
在角度上:
$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();
当我通过Newtonsoft.JsonConvert.SerializeObject
运行模型时,所有格式都正确,日期正是预期的-
年月日
但是当我通过Json()
return通过JsonResult
action方法传递它们时,日期时间如下所示:
大多数MVC结构对我来说都是新的,所以我唯一的猜测是,烘焙到
Json()
中的序列化程序的工作方式与我以前使用的不同。因此,我只能想到两种可能的解决方案:要么找出如何告诉angular如何格式化从JsonResult获得的奇怪日期时间,要么告诉Json使用我以前使用的序列化程序,而不是默认的序列化程序,但我不知道如何完成这两件事,也不知道它们是否是正确的解决方案。referent@StephenMuecke perfect,谢谢。