LINQ TO SQL Classes values resets on deserializing





LINQ TO SQL Classes values resets on deserializing


Recently I was working on a MVC 2 Project. I used JavaScriptSerializer() to serialize my object JavaScriptSerializer().Serialize(object).  The object was a database table in “.dbml” that is “LINQ to SQL Classes”. On searilizing the object it was getting serialized OK and there was no problem at all.

While Deserilaizing was reseting the numeric fileds.  Numeric fields in my table or class were integers. so it was resetting all integer fields to zero.

Initially i thought its something wrong with JavaScriptSerializer().Deserialize

After serializing the object I was sending to one of the my view to put in Query String using ViewData[“varibale”].

Action Code to Serialize

ViewData[“LastSearch”]  = Compression.Compress(new JavaScriptSerializer().Serialize(tblAddress));

View Code
<%=Html.ActionLink(“Delete”, “DeleteAddress”, new { page = item.EGAddressId,pageid = (Model.PageIndex==0)?1:Model.PageIndex ,lastSearch = ViewData[“LastSearch”].ToString() },
new
{
onclick
“return confirm(‘Are you sure you wish to delete this address?’);”})%>

Action Code to DeSerialize

var lastFilter = !string.IsNullOrEmpty(lastSearch)
? new JavaScriptSerializer().Deserialize<tblAddress>(Compression.DeCompress((lastSearch)))
: new tblAddress();

var lastFilter = !string.IsNullOrEmpty(lastSearch)                                ? new JavaScriptSerializer().Deserialize<tblAddress>(Compression.DeCompress((lastSearch)))                                : new tblAddress();

Therefore to save your time create your own model classes where necessary. I have not tried creating partial class but if some one knows a better solution please post here.




Tags: , , ,

Comments are closed.