Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing List of objects via querystring to MVC Controller

Tags:

c#

asp.net-mvc

I've got 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 the querystring. The reason I would want to do this is because this is not a web application, it's a web service that accepts data via the querystring and routes it to a controller that does the work.

So, given a class called MyParam with properties A & B, how can I construct a querystring that will pass data to the following controller method:

public ActionResult MyMethod(List<MyParam> ParamList)

I've tried using the MVC framework to RedirectToAction and RedirectToResult to see what it comes up with, but I assume that my n00bness with MVC is causing me to make a mistake because it never passes the data correctly and MyMethod always has null for the parameter.

Thanks in advance!

like image 365
dudemonkey Avatar asked Apr 27 '11 15:04

dudemonkey


People also ask

How do I pass QueryString?

To pass in parameter values, simply append them to the query string at the end of the base URL. In the above example, the view parameter script name is viewParameter1.

Can we pass data from view to controller?

This blog will discuss four (4) common ways to pass data from the view to the controller: Passing by Typed Arguments. Request Object. Form Collections Object.

Can we use QueryString with post method?

A POST request can include a query string, however normally it doesn't - a standard HTML form with a POST action will not normally include a query string for example.

What is QueryString in MVC?

Generally, the query string is one of client-side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in asp.net mvc. In asp.net mvc routing has support for query strings in RouteConfig.


1 Answers

You may find the following blog post useful for the wire format of lists you need to use if you want the default model binder to successfully parse the request into a strongly typed array of objects. Example of 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 bind to:

public ActionResult MyMethod(IEnumerable<Book> books) { ... }
like image 147
Darin Dimitrov Avatar answered Sep 23 '22 06:09

Darin Dimitrov