Ajax module processing-Analysis of Sina Weibo open source 2

Source: Internet
Author: User

Because Weibo is a system with frequent frontend and backend interactions, Ajax development must also be standardized.

* This system is developed by Asp.net MVC.

First, create two models (one of which is used for paging)

  Public   Class  Jsonmodel {  Public   String Code { Get ; Set  ;}  Public   Object Data { Get ; Set  ;} Public Jsonmodel ( String Code, Object  Data) {code = Code; Data = Data ;}}  Public   Class  Pagerjsonmodel: jsonmodel {  Public   Int Count { Get ; Set ;}  Public Pagerjsonmodel ( String Code, Object Data, Int Count ): Base  (Code, data) {count = Count ;}} 

Code is the processing resultCodeIs an enumerated value. data indicates the actual data to be transmitted.

     Public   Struct  Codestruct {  // Logged On          Public   Static   String Havelogon = "  A00000  "  ;  Public   Static   String Nologin = "  A00001  "  ;  Public  Static   String Nouserexist = "  A00002  "  ;  Public   Static   String Error = "  A00003  "  ;.... 

Then write in the control, for example

PublicJsonresult judgelogin (){If(! Islogin)ReturnNotlogin ();ElseReturnJSON (NewJsonmodel (codestruct. havelogon,New{Name = currentuser. Nickname, id =Currentuser. ID }));}

Data will be returned, for example

 
{"Code": "a00000", "data": {"name": "GeniusProgramMember "," ID ": 6 }}

Frontend call

$ ( Function  () {$. Ajax ({URL: "/Ajax/judgelogin", Datatype: "JSON" , Cache:  False  , Type: "Post" , Success:  Function  (O ){  If (O. Code = "a00000" ){  //  Todo  Alert (Codelist [O. Code]);
} Else { // Todo }});

At the same time, the front end also has corresponding enumeration data, such:

VaRCodelist ={A00000:"Logged On", A00001:"No Logon", A00002:"User does not exist", A00003:"Error. Please try again later",....

So if we want to return some HTML code from the Code rendered by a view and model, I use the following code to convert

 ///   <Summary>         ///  Render the cshtml page as a string  ///   </Summary>          ///   <Param name = "viewname">  Page name  </Param>          ///   <Param name = "model">  Model  </Param>          ///   <Returns> </returns>          Public   String Renderrazorviewtostring ( String Viewname, Object  Model) {viewdata. Model = Model;  Using ( VaR Sw = New  Stringwriter ()){  VaR Viewresult = Viewengines. Engines. findpartialview (controllercontext, viewname );  VaR Viewcontext = New  Viewcontext (controllercontext, viewresult. View, viewdata, tempdata, SW); viewresult. View. Render (viewcontext, SW );  Return  Sw. getstringbuilder (). tostring ();}} 

Then call

 
String data = renderrazorviewtostring ("getcommentshtml", Model );If(!String. isnullorempty (data )){ReturnJSON (NewJsonmodel (codestruct. returnsuccess, data ));}

In this way, the returned HTML data can be filled in a container for display.

Continue to work after completion

Source code release page http://www.cnblogs.com/baichidetiankong/archive/2012/04/17/weibo_source.html

 

Related Article

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.