Sending an array of complex objects to a get string in C # ASP.NET MVC

I want to send an array of objects to the query string of a query. I know this is not an optimal solution, but I just want it to be up and running.

If I have a class, something like this

public class Data { public int a { get; set; } public int b { get; set; } } public class RequestViewData { public IList<Data> MyData { get; set; } } 

I thought I could bind the MVC route to a web request like this

 http://localhost:8080/Request?MyData[0].a=1&MyData[0].b=2&MyData[1].a=3&MyData[1].b=4 

But all this creates an array of two data objects without filling in the values ​​1,2, 3 or 4.

Is there a way to link arrays of complex objects?

+4
source share
2 answers

Assuming you have implemented the GetArrayTest method in your HomeController

 public class HomeController { public ActionResult GetArrayTest (List<Data> data) } 

The following steps will work.

http: // localhost: 8080 / Home / GetArrayTest? Data [0] .a = 1 & Data [0] .b = 1 & Data [1] .a = 2 & Data [1] .b = 2 & Data [2] .a = 3 & Data [2 ] .b = 3

+2
source

I would use BinaryFormatter to create a binary representation of my object, send it Base64 encoded using the query string, and assemble it to the other end.

+1
source

Source: https://habr.com/ru/post/1308138/


All Articles