Is there a way to populate a Dynamic object with Query String parameters?
This is so that my search parameters in the QS can vary without binding them directly to a container object or having to change the signature of the search method.
e.g.
Inbound URL: http://www.test.com/Home/Search?name=john&product=car&type=open&type=all
public ActionResult Search()
{
dynamic searchParams = // **something magic here**
var model = getResults(searchParams);
return View(model);
}
The populated searchParams object should look like:
{
name = "john",
product = "car",
type = { "open", "all" }
}
Any ideas?
One solution can be that you build up an ExpandoObject from the Request.QueryString which is a NameValueCollection.
It’s easy to write the transformation and you can put it inside an extension method:
And in your controller you can use like:
Note: You will need to do some additional transformation to handle to
typeproperty which won’t be automatically an array by default.