1.Dapper return List<dynamic> Object
/// <summary> ///get the paged data recommended by the candidate/// </summary> /// <param name= "page" ></param> /// <param name= "pageSize" ></param> /// <param name= "Candidatename" ></param> /// <param name= "Allrowscount" ></param> /// <returns></returns> Publiclist<Dynamic> Getcandidaterecommendpage (intPageintPageSize,stringCandidatename, out LongAllrowscount) { varsql =@"SELECT c.name,c.birthday,c.cvid from dbo. Candidate C"; varAllrowscountsql =@"SELECT COUNT (0) from dbo. Candidate"; varRET =Base. dbsession.connection.getpage<Dynamic> (page, PageSize, outAllrowscount, SQL,NULL, Allrowscountsql). ToList (); returnret; }2.Controller layer using Newtonsoft.json serialization
public jsonresult getrecommends (int
page, int pageSize, string Candidatename) { var allrowscount = 0l ; var list = RpoJobRepository.NewInstance.GetCandidateRecommendPage ( Page, PageSize, Candidatename, out Allrowscount ); return base . Newtonsoftjson (new {total = allrowscount, rows =
protected Jsonresult Newtonsoftjson (object data, jsonrequestbehavior behavior) { return New Newtonsoftjsonresult { = data, = behavior, }; }
/// <summary> ///newtonsoft Serialization Mode/// </summary> Public classNewtonsoftjsonresult:jsonresult {/// <summary> ///overriding execution View/// </summary> /// <param name= "context" >Context</param> Public Override voidExecuteresult (ControllerContext context) {if(Context = =NULL) { Throw NewArgumentNullException ("Context"); } httpresponsebase Response=context. Httpcontext.response; if(string. IsNullOrEmpty ( This. ContentType)) {response. ContentType= This. ContentType; } Else{Response. ContentType="Application/json"; } if( This. ContentEncoding! =NULL) {Response. ContentEncoding= This. ContentEncoding; } if( This. Data! =NULL) { varjsonstring = Jsonconvert.serializeobject ( This. Data); Response. Write (jsonstring); } }
The 3.View layer is bound like a list of bound objects
columns: [{ true }, { 'Name', ' name ', this. Nameformatter }]
The field here is the same as the column name in the first step.
Summarize
Base of 1.dapper. The Dbsession.connection.getpage<dynamic> method returns a list of Dapperrow, Dapperrow inherits Dictionary<string, object>
2.controller returns the object with JSON serialization, if JavaScriptSerializer is used, the result is the JSON format of the dictionary, such as:
"Total": 143694, "Rows": [[{"Key": "Name", "Value": "Chao Paolong"},{"key": "Birthday", "value": "\/date (107712000000) \"},
Using Newtonsoft.Json.JsonConvert serialization is an object format, such as:
{"Total": 143694, "Rows": [{"Name": "Dizzying tenon?", "Birthday": "1973-06-01t00:00:00", "Cvid": "jr027902222r90250000000"},{" Name ":" 閮 å ãºã? "," Birthday ":" 1981-11-01t00:00:00 "," Cvid ": null},
3. Using dynamic types eliminates the need to define ViewModel classes, saving a lot of time.
4. Another way to do this is to use a DataTable to achieve the same goal without trying.
C # MVC bindings list<dapperrow> to Bootstrap-table list