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!
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.
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.
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.
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.
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) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With