Asynchronously uploads an array from foreground input to the background and into the database
First look at the picture:
Using AJAX asynchronous interaction data, not in the form of a JSON array to the background, but the use of a character array to upload it into the background. Dynamically add each row of data, the corresponding data in each column into an array, and upload to the background for storage. (Of course, you can directly in the form of a JSON array of each row of data to the backstage, here is not detailed)
Front JS Code:
///Save multiple rows of data, using AJAX asynchronous interaction requests functionbtnsave () {/*Value Method One: Add the value of each field in the array, and then convert the array to string strings to the background*/ //defining five one-dimensional arrays varCode =NewArray (); varName =NewArray (); varPWD =NewArray (); varPhone =NewArray (); varBMMC =NewArray (); //add data from a form to the respective array for(vari = 1; I <= Rowl; i++) {Code.push ($ ("#" + "Usercode" + i). Val ());//Corresponding values for each ID in the added formName.push ($ ("#" + "UserName" +i). Val ()); Pwd.push ($ ("#" + "Userpwd" +i). Val ()); Phone.push ($ ("#" + "Phone" +i). Val ()); Bmmc.push ($ ("#" + "BMMC" +i). Val ()); } //Use the JS join method to divide the array into strings, which are used for passing values and parsing in the background. The split symbol is a comma and can be defined by itself. varCode = Code.join (', ')); varName = Name.join (', ')); varPWD = Pwd.join (', '); varPhone = Phone.join (', ')); varBMMC = Bmmc.join (', '); //use Ajax to transmit values, type post$.ajax ({type:"POST", //dataType: "Text",URL: "Userlist.ashx?" Action=save ", data: {usercode:code, Username:name, User Pwd:pwd, Userphone:phone, BMMC:BMMC, Rowlength:rowl//pass the increased number of rows to the background control parameter range in the For loop}, Success:function(Result) {if(Result = = "true"{noty ({text:"Saved successfully!" ", type:" Success ", Layout:" Topcenter ", timeout:2000 }); Refresh (); } Else{noty ({text:"Save failed!" ", type:" Error ", Layout:" Topcenter ", timeout:2000 }); } } }); }
2, the background to obtain the data code:
Private stringHandlesaverequest (HttpContext context) {stringresult ="False"; Try { /*define an array of parameters, get foreground data, and parse the form of arrays. How to use Split ()*/ string[]username = context. request.form["UserName"]. ToString (). Split (','); string[]usercode = context. request.form["Usercode"]. ToString (). Split (','); string[]USERPWD = context. request.form["userpwd"]. ToString (). Split (','); string[]phone = context. request.form["Userphone"]. ToString (). Split (','); string[] Bmmc = context. request.form["Bmmc"]. ToString (). Split (','); intLength =int32.parse (context. request.form["Rowlength"]. ToString ()); Duserlist User=Newduserlist (); Muserlist Muser=Newmuserlist (); //to traverse data in an array into a database for(inti =0; I <length; i++) {Muser.nvfid=System.Guid.NewGuid (). ToString (); Muser.usercode=Usercode[i]; Muser.username=Username[i]; Muser.userpwd=Userpwd[i]; Muser.bmmc=Bmmc[i]; Muser.phoneimei=Phone[i]; //user. Insert (Muser); if(user. Insert (Muser)) {result="true"; } Else{result="False"; } } } Catch(Exception ex) {}returnresult; }
Note: Ajax cannot directly pass an array directly to the background, so convert the array to a string using the Join () method before the array is passed, and then use the Spit () method to split the arrays after the string is fetched in the background.
Asynchronously uploads an array from foreground input to the background and into the database