英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论