添加复杂类型的数组RouteValueDictionary [英] Adding array of complex types to RouteValueDictionary

查看:148
本文介绍了添加复杂类型的数组RouteValueDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有复杂类型数组添加到RouteValueDictionary或兼容型优雅的方式?

I was wondering if there is an elegant way to add an array of complex types to a RouteValueDictionary or compatible type?

例如,如果我有一个类和动作:

For example, if I have a class and an action:

public class TestObject
{
    public string Name { get; set; }
    public int Count { get; set; }

    public TestObject()
    {
    }

    public TestObject(string name, int count)
    {
        this.Name = name;
        this.Count = count;
    }
}

public ActionResult Test(ICollection<TestObjects> t)
{
    return View();
}

后来我知道,如果我通过URL称这种操作/Test?t[0].Name=One&t[0].Count=1&t[1].Name=Two&t[1] .Count之间= 2这将MVC的查询字符串参数映射回ICollection的自动输入。但是,如果我手动创建一个链接的地方使用Url.Action(),我要传递的参数的RouteValueDictionary,当我添加的ICollection到RouteValueDictionary,Url.Action只是使得它作为类型,如&安培; T = System.Collections.Generic.List。

then I know that if I call this action via the URL "/Test?t[0].Name=One&t[0].Count=1&t[1].Name=Two&t[1].Count=2" that MVC will map those query string parameters back into the ICollection type automatically. However, if I am manually creating a link somewhere using Url.Action(), and I want to pass a RouteValueDictionary of the parameters, when I add an ICollection to the RouteValueDictionary, Url.Action just renders it as the type, like &t=System.Collections.Generic.List.

例如:

RouteValueDictionary routeValDict = new RouteValueDictionary();
List<TestObject> testObjects = new List<TestObject>();

testObjects.Add(new TestObject("One", 1));
testObjects.Add(new TestObject("Two", 2));
routeValDict.Add("t", testObjects);

// Does not properly create the parameters for the List<TestObject> collection.
string url = Url.Action("Test", "Test", routeValDict);    

有什么办法让它自动呈现收集成MVC还知道如何映射格式,或我必须手动做到这一点?

Is there any way to get it to automatically render that collection into the format that MVC also understands how to map, or must I do this manually?

我在想什么,他们为什么会做出这么这个美丽的映射存在一个动作而不是提供一种方式来在相反的方向,用于创建网址的手动工作?

What am I missing, why would they make it so this beautiful mapping exists into an Action but not provide a way to manually work in the reverse direction for creating URLs?

推荐答案

我就遇到了这个问题,以及和用Zack的code,但发现一个bug在里面。如果了IEnumerable是字符串的数组(串[])则存在一个问题。所以我thaught我会分享我的扩展版本。

I ran into this problem as well and used Zack's code but found a bug in it. If the IEnumerable is an array of string (string[]) then there is a problem. So i thaught I'd share my extended version.

    public static RouteValueDictionary ToRouteValueDictionaryWithCollection(this RouteValueDictionary routeValues)
    {
        var newRouteValues = new RouteValueDictionary();

        foreach(var key in routeValues.Keys)
        {
            object value = routeValues[key];

            if(value is IEnumerable && !(value is string))
            {
                int index = 0;
                foreach(object val in (IEnumerable)value)
                {
                    if(val is string || val.GetType().IsPrimitive)
                    {
                        newRouteValues.Add(String.Format("{0}[{1}]", key, index), val);
                    }
                    else
                    {
                        var properties = val.GetType().GetProperties();
                        foreach(var propInfo in properties)
                        {
                            newRouteValues.Add(
                                String.Format("{0}[{1}].{2}", key, index, propInfo.Name),
                                propInfo.GetValue(val));
                        }
                    }
                    index++;
                }
            }
            else
            {
                newRouteValues.Add(key, value);
            }
        }

        return newRouteValues;
    }

这篇关于添加复杂类型的数组RouteValueDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆