我有一個產品編輯頁面,允許您添加「相關項目」。訪問該頁面以編輯產品時,它將在服務器上序列化其初始相關項目並存儲在隱藏字段中。當您添加相關項目時,我使用$.ajax()
檢索javascript/jQuery中的項目數組。 $.ajax()
被調用返回像這樣的對象列表的服務器端方法:錯誤反序列化 - 值不能爲空/沒有無參數的構造函數定義
Fyodor.JSON.SearchResult r = new Fyodor.JSON.SearchResult();
DataSet ds = retrieveData();
foreach (DataRow row in ds.Tables[0].Rows)
{
JSON.Product i = new JSON.Product();
//set all Product data in `i`
if (!string.IsNullOrEmpty(i.PartNumber))
{
r.Results.Add(i);
}
}
//set r.TotalPageCount
return r;
當我得到這個數組的項目回到我的javascript,每個項目一直給人成員__type
等於JSON.Product
。當項目初始序列化時,__type
成員不存在。我使用JSON.stringify(my_arrar_of_items)
將新集合序列化並存儲回隱藏字段。
當我保存的產品和檢索隱藏字段的值,並嘗試使用
JavaScriptSerializer s = new JavaScriptSerializer();
relatedItems = s.Deserialize<List<JSON.Product>>(hiddenField.Value);
我得到一個錯誤反序列化:
[ArgumentNullException: Value cannot be null. Parameter name: type] System.Activator.CreateInstance(Type type, Boolean nonPublic) +9635174 System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +431 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +199 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +141 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +231 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +80 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +44 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +74 Shop.UI.Admin.Products.Detail.CreateObject(Product& item) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:720 Shop.UI.Admin.Products.Detail.Save() in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:356 Shop.UI.Admin.Products.Detail.SaveClick(Object sender, EventArgs e) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:121 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
我知道某些錯誤被引起的,因爲這__type
成員。我怎樣才能得到這個字符串反序列化?或者我怎樣才能檢索我的項目$.ajax()
而不是得到會員?
我嘗試過但沒有工作的東西:在一篇SO文章中,我讀到了(現在我找不到它),它表示給出了您正在序列化訪問級別爲protected internal
的對象的構造函數,並且它不會添加__type
成員。該類實際上沒有構造函數或方法,所以我使用該訪問級別創建了一個,並且確實__type
成員不在那裏。但是現在我發現了一個新的錯誤:
[MissingMethodException: No parameterless constructor defined for type of 'Shop.JSON.Product'.] System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +582589 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.ObjectConverter.AddItemToList(IList oldList, IList newList, Type elementType, JavaScriptSerializer serializer, Boolean throwOnError) +87 System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList) +674 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +112 System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +66 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +74 Shop.UI.Admin.Products.Detail.CreateObject(Product& item) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:720 Shop.UI.Admin.Products.Detail.Save() in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:356 Shop.UI.Admin.Products.Detail.SaveClick(Object sender, EventArgs e) in C:\Documents and Settings\path\UI\Admin\Products\Detail.cs:121 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
我不明白,因爲我創造新的構造函數是參..我在做什麼錯?
請發佈*完整和未摻雜的堆棧跟蹤。 –
當然,有你去。 –
你的意思是你爲Product類創建了一個新的無參數構造函數嗎? –