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?

3
  • 3
    Where does JSON and serialization come into this question? Commented Aug 7, 2014 at 18:18
  • I seem to be getting this error when calling JsonConvert.DeserializeObject<List<Control>>(json, _settings) Commented Aug 7, 2014 at 18:21
  • Did you get a solution for this? Commented Sep 21, 2017 at 13:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.