简体   繁体   English

`[FromQuery]` IEnumerable<someobject> 在 ASP.NET Core 3.1 中解析?</someobject>

[英]`[FromQuery]` IEnumerable<SomeObject> parsing in ASP.NET Core 3.1?

So, when I tested how binding works for an IEnumerable<string> argument, you simply pass the argument's name in the query string, repeatedly, like this: ?a=item1&a=item2&a=item3...因此,当我测试IEnumerable<string>参数的绑定如何工作时,您只需在查询字符串中重复传递参数的名称,如下所示: ?a=item1&a=item2&a=item3...

So, what must I write, if I have an argument of type IEnumerable<SimpleObject> a , where SimpleObject is defined as the following:那么,如果我有一个类型为IEnumerable<SimpleObject> a的参数,我必须写什么,其中SimpleObject定义如下:

public class SimpleObject
{
   public string Number { get; set; }
   public string Text { get; set; }
}

in order to successfully bind it to a list of said objects?为了成功地将其绑定到所述对象的列表? Or no such default ModelBinder exists for that mapping?或者该映射不存在这样的默认 ModelBinder? (Please provide a sample ModelBinder in that case) (在这种情况下,请提供一个示例 ModelBinder)

The default model-binding setup supports an indexed format, where each property is specified against an index.默认模型绑定设置支持索引格式,其中每个属性都针对索引指定。 This is best demonstrated with an example query-string:最好用一个示例查询字符串来证明这一点:

?a[0].Number=1&a[0].Text=item1&a[1].Number=2&a[1].Text=item2

As shown, this sets the following key-value pairs如图所示,这设置了以下键值对

  • a[0].Number = 1 a[0].Number = 1
  • a[0].Text = item1 a[0].Text = item1
  • a[1].Number = 2 a[1].Number = 2
  • a[2].Text = item2 a[2].Text = item2

This isn't quite covered in the official docs, but there's a section on collections and one on dictionaries .这在官方文档中并没有完全涵盖,但是有一个关于collections的部分和一个关于dictionaries的部分。 The approach shown above is a combination of these approaches.上面显示的方法是这些方法的组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM