Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass int array to RouteValueDictionary

I need generate this url: http://localhost:3178/Reports/?GroupId=1211&GroupId=1237

I'm trying:

var routeData = new RouteValueDictionary();
routeData.Add("GroupId", "1, 2");

getting: GroupId=1,%202

or

routeData.Add("GroupId", "1");
routeData.Add("GroupId", "2");

getting: An item with the same key has already been added

and even

routeData.Add("GroupId[0]", "1");
routeData.Add("GroupId[1]", "2");

getting: ?GroupId%5B0%5D=1&GroupId%5B1%5D=2

it's possible to somehow fix my issue?

like image 405
Sasha Avatar asked Jun 18 '11 12:06

Sasha


1 Answers

The RouteValueDictionary is meant to provide information to routes. As such, I don't think it has the capability you're asking for. I've been using a custom helper to populate query string data based on what I pass into it:

    public static string BuildPath(RequestContext context, string routeName, RouteValueDictionary routeValues, object additionalParams)
    {
        var vpd = RouteTable.Routes[routeName].GetVirtualPath(context, routeValues);

        if (vpd == null)
            return string.Empty;

        var virtualpath = vpd.VirtualPath;
        var addparams = BuildAdditionalParams(additionalParams);

        if (!virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "?" + addparams;
        else if (virtualpath.Contains("?") && addparams.Length > 0)
            virtualpath = virtualpath + "&" + addparams;

        return "/" + virtualpath;
    }

    protected static string BuildAdditionalParams(object additionalParams)
    {
        if (additionalParams == null)
            return string.Empty;

        StringBuilder sb = new StringBuilder();
        Type type = additionalParams.GetType();
        PropertyInfo[] props = type.GetProperties();

        Action<string, string> addProperty = (name, value) =>
        {
            if (sb.Length > 0)
                sb.Append("&");
            sb.Append(name);
            sb.Append("=");
            sb.Append(value);
        };

        foreach (PropertyInfo prop in props)
        {
            var simplevalue = prop.GetValue(additionalParams, null);
            if (simplevalue != null)
            {
                Type propertyType = prop.PropertyType;
                if (Nullable.GetUnderlyingType(propertyType) != null)
                {
                    propertyType = Nullable.GetUnderlyingType(propertyType);
                }

                if (propertyType.IsEnum) 
                {
                    addProperty(prop.Name, ((int)simplevalue).ToString());
                }
                else if (propertyType.IsArray && propertyType != typeof(string))
                {
                    foreach (var val in prop.GetValue(additionalParams, null) as IEnumerable)                        
                        addProperty(prop.Name, val.ToString());                                                    
                }                    
                else
                {
                    if (!string.IsNullOrEmpty(simplevalue.ToString()))
                        addProperty(prop.Name, simplevalue.ToString());                        
                }
            }
        }
        return sb.ToString();
    }

This function will build a full path to your route and append the values in the additionalParams object as query string data. This can handle arrays, enums, nullable values, and other types that by executing their ToString method.

Hope this helps!

like image 123
bmancini Avatar answered Oct 18 '22 21:10

bmancini