Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a RouteValueDictionary when a route has multiple values?

I am creating a reusable method that inspects my model and automatically builds URLs (via ActionLink) for paging. One of the properties on my model is a string[] (for a multi-select pick list) which is entirely valid. An example of the URL would be: https://example.com?user=Justin&user=John&user=Sally.

However, as the name of the type implies, RouteValueDictionary implements IDictionary so it can't accept the same key more than once.

var modelType = model.GetType();
var routeProperties = modelType.GetProperties().Where(p => Attribute.IsDefined(p, typeof(PagingRouteProperty)));

if (routeProperties != null && routeProperties.Count() > 0) {
    foreach (var routeProperty in routeProperties) {
        if (routeProperty.PropertyType == typeof(String)) {
            routeDictionary.Add(routeProperty.Name, routeProperty.GetValue(model, null));
        }

        if (routeProperty.PropertyType == typeof(Boolean?)) {
            var value = (Boolean?)routeProperty.GetValue(model, null);
            routeDictionary.Add(routeProperty.Name, value.ToString());
        }

        //The problem occurs here!
        if (routeProperty.PropertyType == typeof(string[])) {
            var value = (string[])routeProperty.GetValue(model);
            foreach (var v in value) {
                routeDictionary.Add(routeProperty.Name, v);
            }
        }
    }

//Eventually used here
var firstPageRouteDictionary = new RouteValueDictionary(routeDictionary);
firstPageRouteDictionary.Add("page", 1);
firstPageListItem.InnerHtml = htmlHelper.ActionLink("«", action, controller, firstPageRouteDictionary, null).ToHtmlString();

What can I use to build the routes when a key is needed more than once?

like image 597
Justin Helgerson Avatar asked Dec 09 '14 16:12

Justin Helgerson


People also ask

How do you add a constraint to a route?

Creating Route Constraint to a Set of Specific Values MapRoute( "Default", // Route name "{controller}/{action}/{id}", // Route Pattern new { controller = "Home", action = "Index", id = UrlParameter. Optional }, // Default values for parameters new { controller = "^H.

Can we have multiple routes in MVC?

Multiple Routes You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

Can we add constraints to the route in MVC?

Attribute Routing is introduced in MVC 5.0. We can also define parameter constraints by placing a constraint name after the parameter name separated by a colon. There are many builtin routing constraints available. We can also create custom routing constraints.

How many routes can be defined in MVC?

Every ASP.NET MVC application must configure (register) at least one route in the RouteConfig class and by default, the ASP.NET MVC Framework provides one default route. But you can configure as many routes as you want.


1 Answers

Just try to imagine how the link would look and it should make sense

new RouteValueDictionary { { "name[0]", "Justin" }, { "name[1]", "John" }, { "name[2]", "Sally" } }

which will generate the following query string

Encoded

?name%5B0%5D=Justin&name%5B1%5D=John&name%5B3%5D=Sally

Decoded

?name[0]=Justin&name[1]=John&name[3]=Sally
like image 104
heymega Avatar answered Sep 29 '22 05:09

heymega