Can I have the client and server operate the same JSON object? The way you think about it now is through a client-side hidden control.
The following is a generic list object list<trainingimplement>, which, when converted to JSON, operates on the client and server side
1, JSON object and C # generic conversion code
Copy Code code as follows:
Convert JSON data to generics
public static T convertbytedatatoobject<t> (String bytedata)
{
T obj;
using (var ms = new MemoryStream (Encoding.UTF8.GetBytes (bytedata))
{
var serializer = new DataContractJsonSerializer (typeof (T));
obj = (T) serializer. ReadObject (MS);
}
return obj;
}
Convert generics to JSON
public static string convertobjecttobytedata<t> (T obj)
{
string result;
using (var ms = new MemoryStream ())
{
var serializer = new DataContractJsonSerializer (typeof (T));
Serializer. WriteObject (MS, obj);
Ms. Position = 0;
result = Encoding.UTF8.GetString (Ms. ToArray ());
}
return result;
}
2. The JSON data source is stored in the client-side hidden control
Copy Code code as follows:
<input type= "hidden" id= "Hidedatasource" runat= "Server"/>
3. Note that hiding the control after the JSON data, because contains "/" will make requests for instructions error, so the page header please set validaterequest= "false"
Copy Code code as follows:
<%@ Page language= "C #" validaterequest= "false" autoeventwireup= "true"
4, when the page load Page_Load, initialize the JSON data source
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
#region Load a data source
if (! IsPostBack)
{
list<trainingimplement> list= New list<trainingimplement> () {
New Trainingimplement () {
Code= "AAA",
C_name = "BBB"
}
....
}//Initialization Data source
Hidedatasource.value = Convertobjecttobytedata (list);
}
Else
{
If it is a postback, the data source is read from the client
list<trainingimplement> list = convertbytedatatoobject<list<trainingimplement>> ( Hidedatasource.value);
Hidedatasource.value = Convertobjecttobytedata (list);
}
#endregion
4. Client JS operation JSON data source sample
Copy Code code as follows:
<script type= "Text/javascript" src= ". /scripts/jquery-1.4.3.js "></script>
<script type= "Text/javascript" src= ". /scripts/jquery-ui-1.8.7.custom.min.js "></script>
<script type= "Text/javascript" src= ". /scripts/jquery.json-2.2.min.js "></script>
<script type = "Text/javascript" >
var datasourcehidname = "Hidedatasource";
var datasourcedom;
var Datasourcejson;
$ (document). Ready
(function () {
Get Data source
Datasourcedom = document.getElementById (datasourcehidname);
Datasourcejson = eval ("+ Datasourcedom.value +"));
});
Example method for modifying the code value of the 1th Trainingimplement object
function Modifiedcode () {
Datasourcejson[0]._code = "Code001";
Re-writes a JSON object with the updated value to the hidden control
Datasourcedom.value = $.tojson (Datasourcejson);
}
</script>
5. Service-side C # operations JSON data source
Copy Code code as follows:
list<trainingimplement> list = convertbytedatatoobject<list<trainingimplement>> ( Hidedatasource.value);