ASP.NET MVC传递列表来的RouteData查询字符串 [英] ASP.NET MVC Passing Lists to RouteData QueryString

查看:136
本文介绍了ASP.NET MVC传递列表来的RouteData查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传递值列表作为查询字符串推荐的方法是

The recommended approach for passing lists of values as a QueryString is

www.site.com/search?value=1&value=2&value=3&value=4

ASP.NET处理这口井:

ASP.NET handles this well:

string value = QueryString.Get("value"); // returns "1,2,3,4"

但我不能想出通过将这些值的RouteData的方式。最明显的方法是添加

But I can't figure out a way of passing these values by into RouteData. The obvious approach would be to add

int[] value = {1,2,3,4};

到的RouteData,并有超级智能MVC排序的事情了我。不幸的是MVC是转储,当涉及到传递数组到的RouteData,它基本上调用的ToString()增值= INT []我的查询字符串。

into the RouteData and have super smart MVC sort things out for me. Unfortunately MVC is dump when it comes to passing arrays into RouteData, it basically calls .ToString() adding value=int[] to my QueryString.

我尝试添加值来RouteValueDictionary(但作为一本字典不能处理这个问题:)

I tried adding the values to RouteValueDictionary (but being a dictionary can't handle this:)

RouteValueDictionary dict = new RouteValueDictionary();
dict.Add("value","1");
dict.Add("value","2"); // Throws Exception (Keys must be unique)

我可以尝试传递值是这样的:

I could try passing values like this:

www.site.com/search?value=1,2,3,4

但是这是恩coded到网址为

but this is Encoded to the URL as

www.site.com/search?value=1%2C2%2C3%2C4

我不知道这是否是一件坏事或没有,但它肯定很糟糕。

I'm not sure if this is a bad thing or not,but it sure looks bad.

那么,你如何将值作为中的RouteData ASP.NET MVC的名单。有什么办法,我可以添加到MVC,使其处理一个int []?或者是底层基于字典的数据结构表明,塞传递值列表容易ASP.NET MVC?

So, how do you pass lists of values as RouteData in ASP.NET MVC. Is there any way I can add to MVC to make it handle an int[]? Or is the underlying Dictionary-based data structure a show-stopper for passing lists of values easily to ASP.NET MVC?

推荐答案

我已经通过了一类新拿出一个解决方案,这一点我自己RouteDataList()

I've come up with a solution to this myself by making a new class RouteDataList()

public class RouteDataList<T> : List<T>
{
    public RouteDataList(IEnumerable<T> enumerable) : base(enumerable) { }

    public RouteDataList() : base() { }

    public override string ToString()
    {            
        string output = "";
        for (int i = 0; i < this.Count; i++)
        {
            output += i < this.Count - 1 ? this[i] + "-" : this[i].ToString();
        }

        return output;
    }

    public static List<Int32> ParseInts(string input)
    {
        if (String.IsNullOrEmpty(input)) return null;

        List<Int32> parsedList = new List<int>();
        string[] split = input.Split('-');
        foreach (string s in split)
        {
            int value;
            if(Int32.TryParse(s, out value)) parsedList.Add(value);
        }
        return parsedList;
    }
}

使用如下:

RouteDataList<Int32> valuelist = new RouteDataList<Int32>(){5,6,7,8};
RouteDataList<Int32> anothervaluelist = new RouteDataList<Int32>(){12,13,14,15};

然后传递给带有RouteValueDictionary /匿名类型的功能:

Then Pass to any function that takes a RouteValueDictionary/Anonymous Type:

return RedirectToAction("View", "Browse", new {valuelist, anothervaluelist } );
// Produces http://www.site.com/browse/view?valuelist=5-6-7-8&anothervaluelist=12-13-14-15

// To Parse back to a list:
List<Int32> values = RouteDataList<Int32>.ParseInts(HttpContext.Current.Request.QueryString["valuelist"])

这篇关于ASP.NET MVC传递列表来的RouteData查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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