Home > Net >  How to Convert JSON object to Custom C# type object?
How to Convert JSON object to Custom C# type object?

Time:04-12

I'm trying to post data from JS to MVC controller but the when looking at myobject the items 1-4 are all passed correctly, but the array shows with 3 null values?

can anyone explain where I am going wrong?

Model 1

 public class MyObject
    {
        public List<IDTT> list1 { get; set; }
        public int? item1 { get; set; }
        public int? item2 { get; set; }
        public int? item3 { get; set; }
        public int? item4 { get; set; }
    }

Model 2

public class IDTT
{
    public int ID { get; set; }
}

JSON

{
    "list1": ["1000", "2000", "3000"],
    "item1": "-1",
    "item2": "-1",
    "item3": "-1",
    "item4": "-1"
}

Controller

[HttpPost]
public JsonResult MethodName(MyObject request)
{

CodePudding user response:

list1 is a list of IDTT, not simple ints. Your json would be like this:

"list1": [ {"ID": 1000}, {"ID": 2000}, {"ID":3000}]
  • Related