1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Is it possible to expose POCO types as public properties with Widget user controls?

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

4 posts, 0 answered
  1. Ken
    Ken avatar
    13 posts
    Registered:
    02 Jan 2012
    16 Dec 2011
    Link to this post
    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?
  2. Ken
    Ken avatar
    13 posts
    Registered:
    02 Jan 2012
    16 Dec 2011
    Link to this post
    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.
  3. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    21 Dec 2011
    Link to this post
    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
  4. Ken
    Ken avatar
    13 posts
    Registered:
    02 Jan 2012
    29 Dec 2011
    Link to this post
    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.
4 posts, 0 answered