Sometimes we do a reference type in the background such as:The following entities take C # as an example
Public classorder{ Public stringorderid{Get;Set;} PublicOrderItem orderitem{Get;Set;}} Public classorderitem{ Public stringproductname{Get;Set;}
Public decimalprice{Get;Set;} }
HTML structure of the foreground page:
<formID= "Form1"> <inputtype= "text"name= "ProductName"value= "Milk" /> <inputtype= "text"name= "Price"value= "1.43" /> <inputtype= "Submit"name= "Submit"value= "Submit" /></form>
background When we get orderitem, we expect the data format to be submitted
{orderitem.productname: "", Orderitem.price: ""}
First, we'll recycle the data from the form. Here's a jquery plugin.
;(function($) {$.fn.serializejson=function () { varSerializeobj = {} varArray = This. Serializearray ()varstr = This. Serialize () $ (array). each (function () { if(serializeobj[ This. Name]) { if($.isarray (serializeobj[ This. Name])) {serializeobj[ This. Name].push ( This. Value)} Else{serializeobj[ This. name] = [serializeobj[ This. Name], This. Value]} } Else{serializeobj[ This. Name] = This. Value}}) returnSerializeobj}}) (jQuery);
View Code
var formparam=$ ("#form1"). Serializejson (); Console.log (Json.stringify (formparam))
console Output {"OrderItem": {"productName": "Milk", "Price": "1.45"}}then we think that orderitem.productname must be the object's sub-object to use . OperatorFor example {"UserInfo": {"Clsname": "One", "name": "Li Yang"}} The Name property of the access UserInfo can be used Userinfo.nameOkay, got a solution.
var formparam=$ ("#form1"). Serializejson (); Console.log (Json.stringify (Formparam)) Console.log ($. Param (formparam))
Console output:{"OrderItem": {"productName": "Milk", "price": 1.43}}orderitem%5bproductname%5d=milk&orderitem%5bprice%5d=1.43
If you have a better solution welcome message discussion or join the following QQ group to exchange discussion
Technical Exchange QQ Group: 15129679
Add an entity name to an AJAX form before submitting data