7

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: 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?

3 Answers 3

11

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:

public static class NameValueCollectionExtensions:
{
    public static dynamic ToExpando(this NameValueCollection valueCollection)
    {
        var result = new ExpandoObject() as IDictionary<string, object>;
        foreach (var key in valueCollection.AllKeys)
        {
            result.Add(key, valueCollection[key]);
        }
        return result;
    }
}

And in your controller you can use like:

public ActionResult Search()
{
    dynamic searchParams = Request.QueryString.ToExpando();

    DoSomething(searchParams.name);  
    var model = getResults(searchParams);
    return View(model);
}

Note: You will need to do some additional transformation to handle to type property which won't be automatically an array by default.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree that this would be a nice reusable wrapper to solve the problem.
Having tried this the type property gets parsed as a csv string so it is perfectly usable as is. Thanks!
0

I would rather use dynamic object and do following:

public class QSValues : DynamicObject
    {
        readonly Dictionary<string, object> dynamicProperties = new Dictionary<string, object>();

        public string ErrorMessage { get; set; }

        public bool IsSuccessful { get; set; }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            dynamicProperties[binder.Name] = value;

            return true;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return dynamicProperties.TryGetValue(binder.Name, out result);
        }

and then parse QueryString parameters

1 Comment

I would rather use the accepted answer, but thanks for showing how it would be possible through inheritance.
-1

Something like this would do the trick

dynamic searchParams = new
            {
                name = "john",
                product = "car",
                type = new {aa =  "open", bb = "all"}
            };

Swapping out the strings for your query string values.

1 Comment

Thank you for your answer. I considered this approach but it still binds me to having to know what values to look for when constructing this object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.