Resolving date serialization format problems with the JSON converter that defines the controller for MVC

Source: Internet
Author: User

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:

&LT;SUMMARY&GT;///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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.