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