我會做的是將結果作爲KeyValuePair對象的數組,然後將序列化爲JSON。所以,你創建的JavaScript對象類型,像這樣:
function KeyValuePair(key, value){
this.Key = key;
this.Value = value;
}
,那麼你會建立KeyValuePairs的數組,像這樣:
//This array is declared in the global scope
var ValueArray = new Array();
function someFunction(){
//this assumes that the key and value variables are created earlier in the function
var valueToStore = new KeyValuePair(key, value);
ValueArray[ValueArray.length] = valueToStore;
}
因此,在當你與你所有的檢查,你會做點使用json2 serializer將數組序列化爲json以存儲在隱藏字段中。
var jsonToSaveToHiddenField = JSON.stringify(ValueArray);
//Logic to store resulting json and trigger the serverside evaluation here
在服務器端,您將使用JavascriptSerializer將您的json反序列化爲一個KeyValuePairs數組。這是msdn文檔:JavaScriptSerializer Class Reference
所以這種方法只需要一個隱藏字段。所以你不需要動態地創建它,這應該可以簡化服務器端的檢索。
上面應該用最少的更改工作,但我沒有通過編譯器運行,所以可能會有一些小的語法錯誤預設。