Some commonly used data such as some of the site's regional information is not likely to be changed, generally not through the request, so we choose to exist in a static file, such as the following demo:
1. Dynamically loading JSON data display to foreground
[HttpPost] PublicActionResult GetData () {List<Area> arealist =NewList<area>() { NewArea () {Areaid=1, AreaName="Fujian province", Depth=1, CityList=NewList<area>() { NewArea () {areaid=2, areaname="Fuzhou", depth=2}, NewArea () {areaid=2, areaname="Xiamen", depth=2} } }, NewArea () {Areaid=1, AreaName="Sichuan Province", Depth=1, CityList=NewList<area>() { NewArea () {areaid=2, areaname="Chengdu City", depth=2} } } }; return This. Write (request["Callback"],"",New{success =true, Returndata =arealist}); }
/// <summary> ///Conversion Output/// </summary> /// <param name= "CallBack" ></param> /// <param name= "paramname" ></param> /// <param name= "obj" ></param> /// <returns></returns> PublicContentresult Write (stringCallBack,stringParamName,Objecto) {Newtonsoft.Json.JsonSerializerSettings jsetting=NewNewtonsoft.Json.JsonSerializerSettings (); Jsetting.nullvaluehandling=Nullvaluehandling.ignore; Jsetting.dateformatstring="YYYY-MM-DD HH:mm:ss"; if(CallBack = =""&& ParamName = ="") returnContent (Jsonconvert.serializeobject (o, jsetting),"Application/json"); Else if(CallBack! ="") returnContent (CallBack +"("+ Jsonconvert.serializeobject (o, jsetting) +");","Application/x-javascript"); Else returnContent ("var"+ ParamName +" = "+ Jsonconvert.serializeobject (o, jsetting) +";","Application/x-javascript"); }
<id= "main"> <id= " Ulmain "> </ul></Div >
$.ajax ({URL:'/home/getdata ', DataType:' Jsonp ', type:' Post ', Success:function(r) {varhtml = ' '; for(vari = 0; i < r.returndata.length; i++) { varitem =R.returndata[i]; HTML+ = ' <li> ' + item. AreaName + ' </li> 'varHC = "; if(Item.cityList.length > 0) {HC+ = ' <ul> '; for(varj = 0; J < Item.cityList.length; J + +) { varCity =Item.citylist[j]; HC+ = ' <li> ' + City. AreaName + ' </li> '} HC+ = ' </ul> '; } HTML+=HC; } $("#ulmain"). HTML (HTML); } });
The results are as follows:
2. Generating static files
/// <summary> ///generate static JS file/// </summary> /// <returns></returns> PublicActionResult CreateFile () {List<Area> arealist =NewList<area>() { NewArea () {Areaid=1, AreaName="Fujian province", Depth=1, CityList=NewList<area>() { NewArea () {areaid=2, areaname="Fuzhou", depth=2}, NewArea () {areaid=2, areaname="Xiamen", depth=2} } }, NewArea () {Areaid=1, AreaName="Sichuan Province", Depth=1, CityList=NewList<area>() { NewArea () {areaid=2, areaname="Chengdu City", depth=2} } } }; Newtonsoft.Json.JsonSerializerSettings jsetting=NewNewtonsoft.Json.JsonSerializerSettings (); stringBODY ="var citylist ="+ Newtonsoft.Json.JsonConvert.SerializeObject (arealist, jsetting) +";"; System.IO.File.WriteAllText (Server.MapPath ("~/scripts/temp.js"), body, System.Text.Encoding.UTF8); returnJson (New{success =true, msg ="Build succeeded"}, Jsonrequestbehavior.allowget); }
<id= "btncreate"> generate static js file </ Button >
$ ("#btnCreate"). Click (function () { $.get (function ( r) { if (r.success) { alert ("build succeeded");});} );
Static files in ~/scripts/temp.js:
The contents are as follows:
3. Comment out the AJAX request, point to the static file to read the data
Don't forget to add: <script src= "~/scripts/temp.js" ></script>
4. Final effect display
There is no network request, read the static file, depending on the specific circumstances of the operation.
To save the JSON data in a static script file read