Today, when loading data using the Easyui DataGrid in the MVC Framework, the JSON date format returned by the server is/date (1433088000000+0800)/. The client needs to be further converted. And also does not conform to Easyui frequently uses the date format request, for this reason, has done some research to the controller under the MVC framework. It is found that the problem can be solved by extending the controller's JSON method. It is also possible to satisfy serialization format requirements for whatever type of data by further defining the serialization class itself.
The three-step effort is required to achieve this goal:
1. Create a derived class for the controller. Introduce your own definition Jsonresult
2. Create a derived class of Jsonresult. Implementing your own definition implementation of the JSON date format
3, all need to implement their own definition of the date serialization format of the controller, need to inherit from the controllers of the derived class
The detailed code implements the scale as follows:
<SUMMARY>///implements the implementation of its own definition of the serialization date by overloading the Executeresult method//</summary>public class vmejsonresult:jsonresult{ public override void Executeresult (ControllerContext context) {if (context = = null) {throw n EW 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:con by creating a derived class of controller to introduce its own defined JSON implementation troller{protected override Jsonresult Json (object data, String ContentType, Encoding contentencoding) {Retu RN New Vmejsonresult {data = data, ContentType = ContentType, contentencoding = contentencoding}; } Public New JsonreSult Json (Object data, Jsonrequestbehavior jsonrequest) {return new Vmejsonresult {data = data, Jsonrequestbeh Avior = jsonrequest}; } Public new Jsonresult Json (object data) {return new Vmejsonresult {data = data, Jsonrequestbehavior = Jso Nrequestbehavior.allowget}; }}///<summary>///all need to implement their own controller that defines the effect of the date serialization. Must inherit from 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); }}
Resolving date serialization format problems with the JSON converter that defines the controller for MVC