Coolite cool study 3 MVC + coolite implementation code

Source: Internet
Author: User

Because the default MVC style file contains table and other related styles (H1 ~ H6) Conflicts with coolite, which may lead to gridpanel samples. Remember to put the table and H1 ~ After the H6 style is cleared, you can see the handsome face of the gridpanel...

Project file distribution:

The configuration file of coolite in MVC is the same as that of common webform. However, in the global. asax of MVC, you need to add this sentence to the registerroutes method:

Routes. ignoreroute ("{exclude}/{coolite}/coolite. axd ");

In addition, scriptmanager should specify idmode = "static":

<Ext: scriptmanager id = "scriptmanager1" runat = "server" idmode = "static"/>

The only difference from common MVC is that we need to define our own actionresult to return the JSON result to the client. Because the format required by coolite jsonreader is roughly as follows: {data: [{…}], Totalcount :...}

General Usage of jsonreader:

<Ext: jsonreader readerid = "customerid" root ="Data"Totalproperty ="Totalcount">

Therefore, it is necessary to inherit the abstract method of MVC actionresult public override void executeresult (controllercontext context) and return it to jsonreaderProper tasteJsonresult, otherwise it will not recognize people.

BelowCodeImplements simple encapsulation of JSON response & Save response.

 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web; Using System. Web. MVC; Using Coolite. Ext. Web; Namespace Coolitemvc. helper { Public   Class Ajaxstoreresult: actionresult {Public Ajaxstoreresult (){} Public Ajaxstoreresult ( Object Data ){ This . Data = data ;} Public Ajaxstoreresult ( Object Data, Int Totalcount ): This (Data ){ This . Totalcount = totalcount ;} Public Ajaxstoreresult (storeresponseformat responseformat ){ This . Responseformat = responseformat ;} Private  Object Data; Public   Object Data {get { Return   This . Data;} set { This . Data = Value ;}} Private   Int Totalcount; Public   Int Totalcount {get { Return   This . Totalcount;} set { This . Totalcount = Value ;}}Private Storeresponseformat responseformat = storeresponseformat. load; Public Storeresponseformat responseformat {get { Return   This . Responseformat;} set { This . Responseformat = Value ;}} Private Savestoreresponse saveresponse; Public Savestoreresponse saveresponse {get { If ( This . Saveresponse = Null ){ This . Saveresponse = New Savestoreresponse ();} Return   This . Saveresponse ;}} Public   Override   Void Executeresult (controllercontext context ){ Switch ( This . Responseformat ){ Case Storeresponseformat. Load: String JSON = coolite. Ext. Web. JSON. serialize (data); JSON = "{Data :" + JSON + ", Totalcount :" + 100 +"}" ; Context. httpcontext. response. Write (JSON ); Break ; Case Storeresponseformat. Save: Response response = New Response ( True ); Response. Success = This . Saveresponse. success; response. MSG = This . Saveresponse. errormessage; storeresponsedata saveresponse = New Storeresponsedata (); saveresponse. Confirmation = This . Saveresponse. confirmationlist; response. Data = saveresponse. tostring (); response. Write ();Break ; Default : Throw   New Argumentoutofrangeexception ();}}} Public   Enum Storeresponseformat {load, save} Public   Class Savestoreresponse { Private   Bool Success = True ; Private   String Errormessage; Public   Bool Success {get { Return   This . Success;} set { This . Success = Value ;}} Public   String Errormessage {get { Return   This . Errormessage;} set { This . Errormessage = Value ;}} Public Confirmationlist {Get; Set ;}}}

Use of ajaxstoreresult in customercontroller:

 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web; Using System. Web. MVC; Using System. Web. MVC. Ajax; Using Coolitemvc. models; Using Coolitemvc. helper; Using Coolite. Ext. Web; Namespace Coolitemvc. controllers { Public   Class Customercontroller: controller { //      // Get:/customer/      Public Actionresult index () {viewdata [ "Title" ] = "Customer list" ; Viewdata [ "Message" ] = "Welcome to coolite MVC! My name is Bruce ." ; Return View ();} Public Actionresult list ( Int Limit, Int Start, String Dir, String Sort) {random Rand = New Random (); ilist <customer> List = New List <customer> (); For ( Int I = start; I <start + limit; I ++) List. Add ( New Customer {customerid = "Customer" + I, address = "Address" + I, city = "City" + Rand. Next (1000), companyName = "Com" + Rand. Next (1000), contactname = "Contract" + Rand. Next (1000), contacttitle = "Title" + Rand. Next (1000), Country = "Country" + Rand. Next (1000), email = Rand. Next (1000) + "@ Live.com" , Fax = Rand. next (1, 1000 ). tostring () + Rand. next (1000), mobile = Rand. next (1, 1000 ). tostring () + Rand. next (1000), notes = "Notes" + Rand. Next (1000), phone = "Phone" + Rand. Next (1000), Region = "Region" + Rand. Next (1000), trandate = datetime. Now. adddays (RAND. Next (30 ))}); Return   New Ajaxstoreresult (list, 100 );} Public Actionresult save () {ajaxstoreresult = New Ajaxstoreresult (storeresponseformat. Save ); Try {Storedatahandler datahandler =New Storedatahandler (request [ "Data" ]); Changerecords <customer> DATA = datahandler. objectdata <customer> (); Foreach (Customer In Data. Deleted ){ // DB. Customers. Attach (customer );            // DB. Customers. deleteonsubmit (customer ); } Foreach (Customer In Data. Updated ){ // DB. Customers. Attach (customer );            // DB. Refresh (refreshmode. keepcurrentvalues, customer ); }Foreach (Customer In Data. Created ){ // DB. Customers. insertonsubmit (customer ); }} Catch (Exception e) {ajaxstoreresult. saveresponse. Success = False ; Ajaxstoreresult. saveresponse. errormessage = E. Message ;} Return Ajaxstoreresult ;}}}

Key code of the page:

<Ext: store id = "Dscustomers" Runat = "Server" > <Proxy> <Ext: httpproxy url = "/Customer/List" Method ="Get" /> </Proxy> <updateproxy> <Ext: httpwriteproxy url = "/Customer/Save" /> </Updateproxy> <reader> <Ext: jsonreader readerid = "Customerid" Root = "Data" Totalproperty = "Totalcount" > <Fields> <Ext: recordfield name = "Customerid" Sortdir = "ASC" /> <Ext: recordfield name = "CompanyName" /> <Ext: recordfield name = "Contactname" /> <Ext: recordfield name = "Email" /> <Ext: recordfield name = "Phone" /> <Ext: recordfield name = "Fax" /> <Ext: recordfield name = "Region" /> <Ext: recordfield name = "Trandate" Type = "Date" /> </Fields> </ext: jsonreader> </reader> <baseparams> <Ext: parameter name = "Limit" Value = "15" Mode = "Raw" /> <Ext: parameter name = "Start" Value = "0" Mode ="Raw" /> <Ext: parameter name = "Dir" Value = "ASC" /> <Ext: parameter name = "Sort" Value = "Customerid" /> </Baseparams> <sortinfo field = "Customerid" Direction = "ASC" /> </Ext: store>
 
We can see that the URL is written differently:
 
<Ext: httpproxy url ="/Customer/List"Method ="Get"/>
 
<Ext: httpwriteproxy url ="/Customer/Save"/>
 the detailed page code is similar to the first chapter, which is not listed here. 

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.