c# - How to Host Portable Class Library Class Instance in ViewState -


i have asp.net web-forms application 2 projects: main web project , portable class library project. have following code:

in portable project have item class:

public class item {     public int id { get; set; }      public string name { get; set; } } 

and in default.aspx.cs page have following demo code:

protected void page_load(object sender, eventargs e) {     item item = new item();     item.id = 1;     item.name = "john";     viewstate["mykey"] = item; }  protected void button1_click(object sender, eventargs e) // have button named "button1" on page. {     if (viewstate["mykey"] != null)     {         item item = (item)viewstate["mykey"];         button1.text = item.id + " " + item.name;     } } 

obviously causing error:

type 'portableproject.item' in assembly 'portableproject, version=1.0.0.0, culture=neutral, publickeytoken=null' not marked serializable

while i'm aware of problem , expected solution, i'm not able implement it. solution provide attribute [serializable] item class. however, not possible because portable class library not have system.serializableattribute. i'm aware of this similar question. however, decorating class [datacontract] attribute , members [datamember] has not solved problem (the same error kept showing). apparently view state serializes object in particular way requires functionality [serializable] attribute provides. so, how put instance of item in view state without having preceding error?

edit

i'm still looking solution. portable project consumed cross-platform environments, that's why have keep portable class library. moreover, wish use classes within portable class library in web forms pages (namely viewstate object).

this search appeared promising still couldn't hold of workaround.

you can use json.newtonsoft convert object string. use string within viewstate.

something should work.

 item item = new item();     item.id = 1;     item.name = "john";     viewstate["mykey"] = jsonconvert.serializeobject(item); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -