【问题标题】:RouteValues with a ViewModel带有 ViewModel 的 RouteValues
【发布时间】:2011-10-05 21:44:56
【问题描述】:

我有一个 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 }})"

瞧,另一边渲染的是

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

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

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

有没有办法做我想做的开箱即用的事情? (或不开箱即用)

【问题讨论】:

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


    【解决方案1】:

    试试这样:

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

    不知道你的路由配置是什么样子的,但这可能会在http://theurl?FilterProperty=Product&FilterTerm=ProductA 中产生一些东西。对于您在问题中显示的网址等更具异国情调的内容,您将不得不编写自定义助手。这没什么标准。

    【讨论】:

    • 好吧,太棒了...我过于复杂了...这是我的问题的正确实现...我想我必须在控制器的方法参数中模仿这种结构。 .. 但我没有,我将我的方法参数保留为:MyAction(FilterViewModel filter) 并且它绑定正确...感谢您的帮助,我有时会挂断模型绑定。
    猜你喜欢
    • 1970-01-01
    • 2013-08-06
    • 2020-07-18
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多