dotnet控制器无法从负载中读取嵌套对象。

huangapple go评论136阅读模式
英文:

dotnet Controller Can't read nested object from payload

问题

我的项目基于.NET Framework 4.6.2。我有一个控制器,它接受一个参数,这个参数是一个对象数组。在对象内部有嵌套的对象。

我的前端数据如下:

const customerList = [
  {
    Name: 'John',
    Pets: {
      Cat: false,
      Dog: false,
      Bird: false,
      Mouse: true,
    },
  },
  {
    Name: 'Mary',
    Pets: {
      Cat: false,
      Dog: false,
      Bird: false,
      Mouse: true,
    },
  },
];

const data = {
  customerList,
};

const res = await fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
});

我的后端代码可以循环遍历customerList,可以读取Name的值,例如'John',但无法正确读取嵌套对象"Pets"中的值,它只显示所有的false值,如下所示:

Pets: {
  Cat: false,
  Dog: false,
  Bird: false,
  Mouse: false,
}

我的后端模型如下:

public class Customer
{
    public string Name { get; set; }
    public Pets Pets { get; set; }
}

public class Pets
{
    public bool Cat;
    public bool Dog;
    public bool Bird;
    public bool Mouse;
}

当我尝试访问Pets属性时...

foreach (var customerInfo in customerList)
{
   var name = customerInfo.Name; // 显示正确的值 'John'
   var pets = customerInfo.Pets; // 没有显示正确的值,全部是false
}

我已经搜索了很多次,但找不到类似的示例,我不确定是不是我的关键词不够具体。是否有什么我遗漏的地方?非常感谢!

英文:

My project is based on .Net framework 4.6.2.
I have a controller that takes a parameter, that is an array of objects.
And inside object there is nested object.

My frontend payload looks like this:

const customerList = [
  {
    Name: 'John',
    Pets: {
      Cat: false,
      Dog: false,
      Bird: false,
      Mouse: true,
    },
  },
  {
    Name: 'Mary',
    Pets: {
      Cat: false,
      Dog: false,
      Bird: false,
      Mouse: true,
    },
  },
];

const data = {
  customerList,
};

const res = await fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
});

My backend code can loop through my customerList, and it can read the value from Name like 'John', but it can't read the correct value in the nested object "Pets", it just show all false values like this:

Pets: {
  Cat: false,
  Dog: false,
  Bird: false,
  Mouse: false,
},

My backend model looks like this:

    public class Customer
    {
        public string Name { get; set; }
        public Pets Pets{ get; set; }
    }

    public class Pets
    {
        public bool Cat;
        public bool Dog;
        public bool Bird;
        public bool Mouse;
       
    }
    

and when I trying to access the pets property...

     foreach (var customerInfo in customerList)
     {
       var name = customerInfo.Name; // shows correct value 'John'
       var pets = customerInfo.Pets; // did not show the correct value, all false
    }

I googled many times but can't find a example like me, I wonder is my keyword not specific or not.
Is there anything I am missing? Many thanks!!

答案1

得分: 1

默认的模型绑定器要求具有公共的getter/setter的属性。

如果将Pets类中的字段更改为属性,它将起作用。

public class Pets
{
    public bool Cat { get; set; }
    public bool Dog { get; set; }
    public bool Bird { get; set; }
    public bool Mouse { get; set; }
}

否则,如果您想保持它们作为字段,您还可以实现自己的模型绑定器。也许这个问题会对您有所帮助

英文:

The default model binder requires properties with public getter/setter.

If you change your fields in Pets class to properties, it will work.

public class Pets
{
    public bool Cat { get; set; }
    public bool Dog { get; set; }
    public bool Bird { get; set; }
    public bool Mouse { get; set; }
}

Otherwise, you could also implement your own model binder, if you want to keep them as fields. Maybe this question will help you

huangapple
  • 本文由 发表于 2023年3月7日 00:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定