0
 public interface ISurvey
{
    List<ISurveyItem> Items { get; set; }
    int QueueId { get; set; }
    SurveyType Type { get; set; } //an enum

    SurveyResult ToSurveyResult();
    void CopyToSurveyDto(Survey dbsurvey);
}

public interface ISurveyItemBase
{
    int Sequence { get; set; }
    string Template { get; set; }
    string Label { get; set; }
    int Id { get; set; }
}

public interface ISurveyItem:ISurveyItemBase
{
    SurveyItemType Type { get; set; }//an enum
    string Value { get; set; }
}

public class Survey : ISurvey
{
    public List<ISurveyItem> Items { get; set; }
    public int QueueId { get; set; }
    public SurveyType Type { get; set; }

    public SurveyResult ToSurveyResult()
    {
       //implimentation
    }

    public void CopyToSurveyDto(Data.Survey dbsurvey)
    {
        //implimentation
    }
}

When I try pass the Survey object via POST to my Web API service, the Items property is serialized to an empty list. I imagine this has to do with it not knowing what concrete type to serialize the items too. Are there any pointers on how to do this?

Mike_G
  • 16,237
  • 14
  • 70
  • 101
  • What is the definition of `ISurveyItem`? – D Stanley Apr 01 '14 at 20:17
  • One possible answer [here](http://stackoverflow.com/questions/15880574/json-net-how-to-deserialize-collection-of-interface-instances). – D Stanley Apr 01 '14 at 20:18
  • Added ISurveyItem interface for you. That link does look like the path to an answer, I just don't know how to implement that on my Web API method. – Mike_G Apr 01 '14 at 20:35

1 Answers1

0

In order to accomplish this, I had to refactor a little bit:

public interface ISurvey<T>
    where T: ISurveyItem 
{
    List<T> Items { get; set; }
    int QueueId { get; set; }
    SurveyType Type { get; set; }
}

public class Survey : ISurvey<SurveyItem>
{
    public List<SurveyItem> Items { get; set; }
    public int QueueId { get; set; }
    public SurveyType Type { get; set; }
}

credit source

Mike_G
  • 16,237
  • 14
  • 70
  • 101