Today, when loading data using the Easyui DataGrid in the MVC Framework, the JSON date format returned by the server is/date (1433088000000+0800)/, requires further conversion by the client, and does not conform to the Easyui commonly used date format requirements. , this paper studies the controller under the MVC framework, discovers that the problem can be solved by extending the controller's JSON method, and can satisfy the serialization format requirement of any kind of data by further customizing the serialization class.
To achieve this goal, you need to complete the three-step process:
1. Create a derived class for the controller and introduce the custom Jsonresult
2. Create a derived class of Jsonresult to implement a custom implementation of the JSON date format
3. All controllers that need to implement a custom date serialization format must inherit from the controller's derived class
Examples of specific code implementations are:
<summary>///implementation of custom serialization dates by overloading the Executeresult method//</summary>public class vmejsonresult:jsonresult{P ublic override void Executeresult (ControllerContext context) {if (context = = null) {throw NE W ArgumentNullException ("context"); } httpresponsebase response = context. Httpcontext.response; if (this. Data! = null) {jsonserializersettings setting = new Jsonserializersettings (); Sets the format of the date serialization setting. dateformatstring = "Yyyy-mm-dd HH:MM:ss"; Response. Write (Jsonconvert.serializeobject (Data, setting)); }}}///<summary>/////</summary>public class Vmecontroller:cont by creating a derived class of controller to introduce a custom JSON implementation roller{protected override Jsonresult Json (object data, String ContentType, Encoding contentencoding) {retur N new Vmejsonresult {data = data, ContentType = ContentType, contentencoding = contentencoding}; } Public New JsonresuLt Json (Object data, Jsonrequestbehavior jsonrequest) {return new Vmejsonresult {data = data, Jsonrequestbehav IOR = jsonrequest}; } Public new Jsonresult Json (object data) {return new Vmejsonresult {data = data, Jsonrequestbehavior = Jso Nrequestbehavior.allowget}; }}///<summary>///all controllers that need to implement a custom date serialization effect must inherit from the vmecontroller///</summary>public class Couponcontroller: vmecontroller{public ActionResult Index () {return View (); } public ActionResult Getallcoupontypes () {Hashtable Hashtable = new Hashtable (); hashtable["sessionId"] = ""; String json = Jsonhelper.serialize (hashtable); String Retjson = Httphelper.postforjson ("Http://localhost/vme", "Couponservice.svc", "Getallcoupontypes", JSON); String jsonresult = Jsonhelper.getstring (Retjson); list<coupontype> results = jsonhelper.deserializeobject<list<coupontype>> (JsonResult); Return JSON (results, JSONRequestbehavior.allowget); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Resolving date serialization format problems with a JSON converter that customizes the controller of MVC