传递到 Html.ActionLink 时序列化模型上的 IList 属性
我正在尝试使用以下视图模型生成 Html.ActionLink:
public class SearchModel
{
public string KeyWords {get;set;}
public IList<string> Categories {get;set;}
}
要生成链接,我使用以下调用:
@Html.ActionLink("Index", "Search", Model)
其中 Model 是 SearchModel 的实例
生成的链接如下所示:
http://www.test.com/search/index?keywords=bla& categories=System.Collections.Generic.List
因为它显然只是对每个属性调用 ToString 方法。
我希望看到生成的是:
http://www.test. com/search/index?keywords=bla&categories=Cat1&categories=Cat2
有什么方法可以通过使用 Html.ActionLink
来实现此目的
I'm trying to generate an Html.ActionLink with the following viewmodel:
public class SearchModel
{
public string KeyWords {get;set;}
public IList<string> Categories {get;set;}
}
To generate my link I use the following call:
@Html.ActionLink("Index", "Search", Model)
Where Model is an instance of the SearchModel
The link generated is something like this:
http://www.test.com/search/index?keywords=bla&categories=System.Collections.Generic.List
Because it obviously is only calling the ToString method on every property.
What I would like to see generate is this:
http://www.test.com/search/index?keywords=bla&categories=Cat1&categories=Cat2
Is there any way I can achieve this by using Html.ActionLink
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 MVC 3 中,您只是运气不好,因为路由值存储在
RouteValueDictionary
中,顾名思义,它在内部使用Dictionary
,这使得不可能有多个与单个键关联的值。路由值可能应该存储在NameValueCollection
中,以支持与查询字符串相同的行为。但是,如果您可以对类别名称施加一些限制,并且能够支持以下格式的查询字符串:
那么理论上您可以将其插入到
Html.ActionLink
中,因为 MVC 使用TypeDescriptor
反过来又可以在运行时扩展。下面的代码是为了证明它是可能的,但我不建议使用它,至少在不进一步重构的情况下。话虽如此,您需要首先关联自定义类型描述提供程序:
提供程序的实现和覆盖
Categories
属性的属性描述符的自定义描述符:然后我们需要自定义属性描述符能够在
GetValue
中返回自定义值,该值由 MVC 内部调用:最后证明它可以运行一个模仿 MVC 路由值创建的示例应用程序:
该死,这是可能是我有史以来最长的答案给这里SO。
In MVC 3 you're just out of luck because the route values are stored in a
RouteValueDictionary
that as the name implies uses aDictionary
internally which makes it not possible to have multiple values associated to a single key. The route values should probably be stored in aNameValueCollection
to support the same behavior as the query string.However, if you can impose some constraints on the categories names and you're able to support a query string in the format:
then you could theoretically plug it into
Html.ActionLink
since MVC usesTypeDescriptor
which in turn is extensible at runtime. The following code is presented to demonstrate it's possible, but I would not recommend it to be used, at least without further refactoring.Having said that, you would need to start by associating a custom type description provider:
The implementation for the provider and the custom descriptor that overrides the property descriptor for the
Categories
property:Then we would need the custom property descriptor to be able to return a custom value in
GetValue
which is called internally by MVC:And finally to prove that it works a sample application that mimics the MVC route values creation:
Damn, this is probably the longest answer I ever give here at SO.
当然是用 linq...
with linq of course...