I and Xiaomei QR code diary (3) one sentence to deal with MVC form page data binding and submission, Midea mvc
"Nima, are you free today ?"
"Today's Sunday, why is it unavailable? Are you making an offer? I eat all the spicy and cool skins. I'll be there soon ."
"You asked me about your sister. I added a class for one day yesterday. Things haven't been finished yet. Please come over and hurry up. I need you !"
"Don't worry. If you have a brother, this is not a problem ."
20 minutes later...
"Xiaomei, what's wrong ?"
"Nima Ge, MVC is not easy to use at all. I used to use WebForm to drag server controls in my graduation project. I am working on Order Management these two days. I used to use server controls and write them after dragging them"
private void save(){string orderNo=txt_OrderNo.Text.Trim();string orderDate=txt_OrderDate.text.trim();... ...SqlHelper.Execute("Insert Into Order...... ...}
"When the table header data is submitted and written in this way, the Order details are better handled. You can drag a GridView to bind the data. You can see the interface. I finished it yesterday morning. As you said before, the data is in Json format and submitted to the backend Using Ajax. The data is parsed one by one in the backend, inserting the database will drive me crazy. The header is okay, and the details will drive me crazy !! You can see that this is the interface I dragged out like other interfaces"
"Good. The good interface is very good now, And JQgrid is similar. How did you write the code for submitting data ?"
"No, you see"
// Save the event function SubmitOrderForm () {function (r) {if (r) {Loading (true, "submitting data... "); window. setTimeout (function () {var postData = "{[" OrderNo: "+ $ (txt_OrderNo ). val () + ...... // I don't know how to write in the list !!!!! AjaxJson ("/ExampleModule/Order/SubmitOrderForm? KeyValue = "+ KeyValue, postData, function (data) {tipDialog (data. message, 3, data. code); ThisCloseTab ();}) ;}, 200 );}});}
"It looks like this. The order list shows the same order to get the value in Json format. Why not ?"
"In the table, I do not know how many rows of data the user has filled in. If I do, I can write a for loop ."
"Xiaomei, I don't know how deep your career is. Why are you so stupid! You can see the number of rows in a table. Isn't it good for you to go through the loop like this ?"
"But you didn't see it. Isn't it free !"
"They all say that women have big breasts and no brains. I think you are still not developing, and you will definitely be better than Liu Yan in the future. If you have a blank line, you can skip the loop! This code is too messy to write, and even if it is finished, it is easy to cause exceptions. I will give you a function to retrieve page data and you can use it on your own"
/* Automatically obtain the page control value */function GetWebControls (element) {var reVal = ""; $ (element ). find ('input, select, textarea '). each (function (r) {var id = $ (this ). attr ('id'); var value = $ (this ). val (); var type = $ (this ). attr ('type'); switch (type) {case "checkbox": if ($ (this ). attr ("checked") {reVal + = '"' + id + '"' + ':' + '"1 ", '} else {reVal + =' "'+ id +'" '+': '+' "0", '} break; default: if (value = "") {value = "& nbsp;" ;}reval + = '"' + id + '"' + ':' + '"' + $. trim (value) + '", 'break ;}}); reVal = reVal. substr (0, reVal. length-1); return jQuery. parseJSON ('{' + reVal + '}');}
"Nima brother, I really can't see it. Don't look at you. The code you wrote is really flexible. Just a few words, I just got into the code, Alert, the control and value on the interface are all combined into Json. I only need to change the control name to the field name in the background, and then I can directly call the background method to convert Json into an object and directly Insert the database."
"Xiaomei, you have got the disease of Liu Yan, but you have no life! Why are you so stupid? The front-end code is also written by you. The control name on your interface is the same as the field name, so you don't need to change it. Json is submitted to the backend, and the object is inserted into the database in one breath, you can directly close it to a public method."
"Some code is generated using a code generator !"
"Won't you change the code generator! Generate txt _ and select _ on the front-end interface _... It's not good to remove these damn things. No matter how many controls are added to the code, you don't need to change the code !"
"Well, too. I'll write it first ."
An hour later ......
"Nima Ge, okay, it's really good to use the code to directly close it to the function library and submit it to the background with just one sentence ."
"You have been tossing for an hour. You are also a talent ."
"Cut, I submitted the details and completed the form submission. I used to be unfamiliar with JS. In fact, JS is the same as C #. It can also be traversed, and the control can be assigned a value, the same concept of object. After I converted the details into Json, the back-end will be converted to List, and batch Inserts will facilitate transactions. In fact, page editing is an operation with the opposite values, that is, getting the value from the background, converting the object to Json, and then changing your method, that's fine"
/* Automatically assign a value to the control */function SetWebControls (data) {for (var key in data) {var id = $ ('#' + key); var value = $. trim (data [key]). replace ("& nbsp;", ""); var type = 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 ;}}}
"Encapsulate these two functions. you can load or save data on the front-end page in one sentence, Nima! I wrote the code this time. Do you want to send a copy to your friends"
"Don't, your code is directly used in the project. Some company Logo links in the project are easy to misunderstand and promote ."
"Well, okay. If I have time later, I will remove all the company and project information and upload the source code ."