ASP.NET MVC 将列表传递给 RouteData QueryString
将值列表作为 QueryString 传递的推荐方法是
www.site.com/search?value=1&value=2&value=3&value=4
ASP.NET 可以很好地处理这一点:
string value = QueryString.Get("value"); // returns "1,2,3,4"
但我无法找出将这些值传递到 RouteData 的方法。显而易见的方法是添加
int[] value = {1,2,3,4};
到 RouteData 中,并让超级智能的 MVC 为我解决问题。不幸的是,MVC 在将数组传递到 RouteData 时是转储的,它基本上调用 .ToString() 将 value=int[] 添加到我的 QueryString 中。
我尝试将值添加到 RouteValueDictionary (但作为字典无法处理这个:)
RouteValueDictionary dict = new RouteValueDictionary();
dict.Add("value","1");
dict.Add("value","2"); // Throws Exception (Keys must be unique)
我可以尝试传递这样的值:
www.site.com/search?value=1,2,3,4
但这是编码到 URL 的,因为
www.site.com/search?value=1%2C2%2C3%2C4
我不确定这是否是一件坏事,但是它看起来确实很糟糕。
那么,如何在 ASP.NET MVC 中将值列表作为 RouteData 传递。有什么方法可以添加到 MVC 以使其处理 int[] 吗?或者,基于字典的底层数据结构是否是一个将值列表轻松传递到 ASP.NET MVC 的阻碍?
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 handles this well:
string value = QueryString.Get("value"); // returns "1,2,3,4"
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};
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.
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
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己想出了一个解决方案,通过创建一个新类 RouteDataList()
使用如下:
然后传递给任何采用 RouteValueDictionary/Anonymous 类型的函数:
I've come up with a solution to this myself by making a new class RouteDataList()
Use as follows:
Then Pass to any function that takes a RouteValueDictionary/Anonymous Type: