Home > OS >  How to bind a collection with objects?
How to bind a collection with objects?

Time:05-26

I need to bind a collection of objects from a querystring, but I cannot find the proper querystring format.

My controller code:

public class Filter
{
    public string Name { get; set; }
    public string Operator { get; set; }
    public object Value { get; set; }
}

public void Get(IEnumerable<Filter> filters)
{
     ....
}

CodePudding user response:

If you do want to pass the objects with querystring you could try as below:

https://localhost:44389/Test/Index?filters[0].Name=n1&filters[1].Name=n2&filters[2].Name=n3&filters[2].Value=v3

The result: enter image description here enter image description here

the offcial document related:

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0#collections

but i don't think it's a good solution,because the length of Url is limited,if your model has plenty properties and your collection has many elements ,you may get some error

  • Related