Me and Amy's Code diary (3) One sentence to fix MVC form page data binding and submission

Source: Internet
Author: User

"Are you free today, brother?" ”

"Today Sunday Ah, how will not be free, you this is an offer?" I eat the cold soup and I'll be there in a minute. ”

"About your sister, I have to add a day yesterday, things have not finished, you give me to come quickly, I need you!" ”

"Don't worry, oh, there's a brother here, it's not a thing." ”

20 minutes later ...

"Mei, what's the problem?" ”

"My brother, MVC, this is a broken thing, not good at all, I used to do graduation design with WebForm inside drag server control can be convenient. I've been doing this for two days. Order management, previously with the server control, after dragging the control to write "

Private void Save () {string orderno=Txt_OrderNo.Text.Trim (); string orderdate=Txt_OrderDate.text.trim (); Sqlhelper.execute ("Insert into Order ... ... ...}

"Table header data submitted so write, order details better get, directly drag a GridView out data can be bound up." You look at the interface I was ready yesterday morning, the data according to what you have said, the composition of JSON to go to the background of Ajax submission to the backstage, and then a solution, and then plug the database to make me crazy, the table head of the good, details will make me crazy!! You see, this is the interface I dragged out of other interfaces.

"Good, good interface now painting very good, jqgrid use also very similar to the matter." How do you write the code for the data you are submitting? ”

"Connaught, you See"

    //Save Event    functionSubmitorderform () {function(r) {if(R) {Loading (true, "Submitting data ..."); Window.settimeout (function () {                    varPostData = "{["OrderNo: "+$ (Txt_orderno). val () + ...///The schedule doesn't know how to write!!!!! Ajaxjson ("/examplemodule/order/submitorderform?" Keyvalue= "+ KeyValue, PostData, function (data) {Tipdialog (data). Message, 3, data.                        Code);                    Thisclosetab ();                }); }, 200);    }        }); }

"It looks like this, the order list, the same as the value out to spell JSON AH why not?" ”

I don't know how many rows of data the user has filled in the form, and I can write a for loop if I know. ”

"Mei, I see your career line depth is not how Ah, you this brain is still so stupid!" How many lines do you have in the form? Do you want to recycle? ”

"But you didn't see that there was no space on it, okay?" ”

"All said that women have no brains, I think you must still have no development, the future is certainly better than the spokesperson body." If you have a blank line, you can jump out of the loop. You write this code is too messy, even if it is easy to get out of the exception, I give you a page data function, you take it with the "

/*automatically get page control values*/functionGetwebcontrols (Element) {varReVal = ""; $ (Element). Find (' Input,select,textarea '). each (function(r) {varID = $ ( This). attr (' id '); varValue = $ ( This). Val (); varType = $ ( This). attr (' type '); Switch(type) { Case"checkbox":                if($( This). attr ("Checked") ) {ReVal+ = ' + id + ' + ' + ': ' + ' "1", '                } Else{ReVal+ = ' + id + ' + ' + ': ' + ' "0", '                }                 Break; default:                if(Value = = "") {Value= " "; } reVal+ = ' + id + ' + ' + ': ' + ' + ' + $.trim (value) + ' ", ' Break;    }    }); ReVal= Reval.substr (0, Reval.length-1); returnJquery.parsejson (' {' + ReVal + '} ');}

"My brother, really do not see AH, do not look at you are not good, write out the code is really flexible, on these few words, I just put into the code alert, the interface control and value all into the JSON, I just in the background to change the control name to a field name, you can directly adjust the background method to convert JSON into a solid, Plug in the database directly. ”

"Mei, you really got spokesperson's disease no spokesperson life!" Why are you so stupid, the foreground code is also you write, your interface control name and field name consistent, do not have to go, JSON submitted backstage, to the entity plug database one go, directly can be closed into a common method. ”

"Some code I generated with the code generator!" ”

"You won't change the code generator!" Generate Txt_, select_ on the foreground interface. These damn things get rid of it, no matter how to add control to control the code, a code does not have to change it! ”

"Well, also oh, I write and read first." ”

An hour later ...

"My brother, well, that code really good use I directly closed to the function library, submitted backstage on a sentence." ”

"You've been doing it for an hour, and you're a talent," he said. ”

"Cut, I put the details submitted, the form submitted together, the former is I JS used not familiar, in fact, JS also with C #, can also traverse the same can be assigned to the value of the control, as well as the concept of objects. I have just turned the details into JSON, the background to the list, bulk INSERT, with the transaction is also convenient. Page editing is actually the inverse of the operation, that is to take the value from the background, the entity into JSON, and then you change the method so that it is good "

/*assign a value to a control automatically*/functionSetwebcontrols (data) { for(varKeyinchdata) {        varid = $ (' # ' +key); varValue = $.trim (Data[key]). Replace (" ", "" "); varType = id.attr (' type ')); Switch(type) { Case"checkbox":                if(Value = = 1) {id.attr ("Checked", ' checked '); } Else{id.removeattr ("Checked"); }                $(' Input '). Custominput ();  Break; default: Id.val (value);  Break; }    }}

"To encapsulate the two functions, the front page to load data or save data, a word can be done, ah, brother!" This code is I write good, want to send a copy to the Netizen?

"Don't, you that code is directly used in the project, the project inside some company logo link what, easy to let others misunderstanding is to engage in promotion." ”

"Well, OK, later I have time, the company, the project information completely removed, and then upload the source code." ”

Me and Amy's Code diary (3) One sentence to fix MVC form page data binding and submission

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.