Passing a List of Objects via querystring to MVC Controller

I have a situation where I need to pass a list of objects to an MVC controller, but I'm not sure how to format this in querystring. The reason I would like to do this is because it is not a web application, it is a web service that receives data through a request and directs it to a controller that does this work.

So, given the class MyParam with properties A and B, how can I build a query that will pass data to the next controller method:

public ActionResult MyMethod(List<MyParam> ParamList) 

I tried using the MVC environment for RedirectToAction and RedirectToResult to understand what this is connected with, but I assume that my n00bness with MVC makes me wrong, because it never passes the data correctly, and MyMethod always is null for the parameter.

Thanks in advance!

+6
source share
3 answers

You can find the following blog post , useful for the list wire format you need to use if you want the standard connecting device to successfully parse the request into a strongly typed array of objects. Example query string:

 [0].Title=foo&[0].Author=bar&[1].Title=baz&[1].Author=pub... 

Where:

 public class Book { public string Title { get; set; } public string Author { get; set; } } 

will successfully communicate with:

 public ActionResult MyMethod(IEnumerable<Book> books) { ... } 
+9
source

Well, based on the information provided, I don’t think you want what you want. In your case, on the client, you send data to the controller. Do not use request.

ok, since you need to use querystring. my new answer is: serialize the object, convert it to a base64 string and pass it, then convert back.

0
source

I found that JsonValueProvider works much better than the regular ValueProvider for binding to a list. Just convert the data to a JSON object as follows:

 <YourRoute>?ParamList=[{SomeProperty:'Value'},{SomeProperty:'Value'}]; 

And JsonValueProvider will take care of the rest. This suggests that you can say β€œsend this data as Json”.

I also deny it was a good idea.

0
source

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


All Articles