1

I am having an issue deserializing my SelectList inside an object. My class is set up like so:

public class DropDownControl
{
    public string Type
    {
        get { return "ddl"; }
    }
    public SelectList Values { get; set; }
}

The object I have created is simply

var myControl = new DropDownControl()
{
    Values = new SelectList(
        new List<SelectListItem>
        {
            new SelectListItem {Value = "1", Text = "A"},
            new SelectListItem {Value = "2", Text = "B"},
            new SelectListItem {Value = "3", Text = "C"},
        }, "Value", "Text", "1"
    )
}

and I am getting the error

Cannot create and populate list type System.Web.Mvc.SelectList.

When calling

JsonConvert.DeserializeObject<List<Control>>(myControl, _settings);

Certainly there has to be some way to serialize/deserialize a SelectList?

Barry Tormey
  • 2,966
  • 4
  • 35
  • 55

1 Answers1

1

Because SelectList has no default (i.e. parameterless) constructor, you're going to need to write a custom converter in order for JSON.NET to know how to instantiate it. This might help you learn how to do that.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315