带有ViewModel的RouteValues

时间:2011-10-05 21:44:57

标签: asp.net-mvc-3 model-view-controller routedata

我有一个ViewModel,它是一个非常简单的过滤器对象,如下所示:

public class FilterViewModel
{
    public String FilterTerm { get; set; }
    public String FilterProperty { get; set; }
}

我希望做的是从另一个页面到这个页面的路由链接,并将我的FilterViewModel传递到路由URL创建到RouteValues中,如下所示:

Url.RouteUrl("myRoute", new { filter = new FilterViewModel() { FilterProperty = "Product", FilterTerm = _detail.FilterTerm }})"

Lo,另一方呈现的是

http://theurl?filter=Fully.Qualified.Namespace.FilterViewModel

我不知道我的期望,也许是像这样序列化到查询字符串中的东西:

http://theurl?filter=FilterProperty|Product,FilterTerm|ProductA

反正有没有做我想要开箱即用的事情? (或不开箱即用)

1 个答案:

答案 0 :(得分:1)

试试这样:

Url.RouteUrl(
    "myRoute", 
    new { 
        FilterProperty = "Product", 
        FilterTerm = _detail.FilterTerm 
    }
)

不知道您的路由配置如何,但这可能会产生http://theurl?FilterProperty=Product&FilterTerm=ProductA行之类的内容。对于任何更具异国情调的东西,比如您在问题中显示的网址,您将不得不编写自定义帮助程序。这没什么标准。