Is it possible to expose POCO types as public properties wit

Posted by Community Admin on 04-Aug-2018 21:40

Is it possible to expose POCO types as public properties with Widget user controls?

All Replies

Posted by Community Admin on 16-Dec-2011 00:00

I have a widget where I want to display 3 featured items. The content will be kept in the CMS, so I'd like to keep them as public properties that users can edit. Right now I have it as:

public string FeaturedBootStyle1 get;set;
public string FeaturedBootStyle2 get;set;
public string FeaturedBootStyle3 get;set;
public Guid FeaturedBootStyle1ImageId get;set;
public Guid FeaturedBootStyle2ImageId get;set;
public Guid FeaturedBootStyle3ImageId get;set;
public string FeaturedBootStyle1Headline get;set;
public string FeaturedBootStyle2Headline get;set;
public string FeaturedBootStyle3Headline get;set;
public string FeaturedBootStyle1Copy get;set;
public string FeaturedBootStyle2Copy get;set;
public string FeaturedBootStyle3Copy get;set;

etc. There are a few other properties for each. Ideally, I would like to refactor it into something like this:

public class FeaturedBoot
  public string StyleNumber get;set;
  public string Headline get;set;
  public string Copy get;set;
  public Guid ImageId get;set;
 
public FeaturedBoot Boot1 get;set;
public FeaturedBoot Boot2 get;set;
public FeaturedBoot Boot3 get;set;

or better yet...
public FeaturedBoot[] FeaturedBoots get;set;

However, when I tried adding these as properties, in my control designer javascript file, I did a console.log() of the data object that was passed to it, and it simply returned the a string of the object's type as the value of the property.

Do I need to do something special to allow this object to be serialized using JSON?

Posted by Community Admin on 16-Dec-2011 00:00

I discovered that if you override ToString() method in the POCO, that SiteFinity will set the property in the javascript using the ToString() output.

Is there a way to control Serialization for this object? I'd like to just use standard JSON serialization of this object.

Posted by Community Admin on 21-Dec-2011 00:00

Hi,

To serialize our object to JSON using the DataContractJsonSerializer we must either mark it with the Serializable attribute or the DataContract attribute. If we mark the class with the DataContract attribute, then we must mark each property we want serialized with the DataMember attribute.

[Serializable]
public class Person
    public Person()
    public Person(string firstname, string lastname)
    
        this.FirstName = firstname;
        this.LastName = lastname;
    
    public string FirstName get; set;
    public string LastName get; set;
 
 
/// Marked with the DataContact Attribute
[DataContract]
public class Person
    public Person()
    public Person(string firstname, string lastname)
    
        this.FirstName = firstname;
        this.LastName = lastname;
    
 
    [DataMember]
    public string FirstName get; set;
 
    [DataMember]
    public string LastName get; set;

 

Greetings,
Stanislav Velikov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 29-Dec-2011 00:00

Stanislav:

That solution didn't work. I added the DataContract and DataMember attributes to my POCO class and it's still being serialized to javascript as a string, not as a javascript object like I'd expect.

This thread is closed