【问题标题】:Mvc4 Model to url querystringMvc4模型到url查询字符串
【发布时间】:2013-11-26 10:05:30
【问题描述】:

我正在寻找一种将 mvc4 模型转换为查询字符串的方法。 mvc4 的内置机制允许我做这样的事情:

@Url.Action("SearchWithQueryString","Search", new {@Title = "Title", @Author= " Author", @Date = "date"})

这个命令的结果是:

Url/Search/SearchWithQueryString?Title=title&Author=author&date=date

我的目标是传递一个 poco 模型并得到相同的结果。 例如,如果我有这个类:

public class Test
{
    public string Title {get;set;}
    public string Author {get;set;}
    public string Date {get;set;}
}

我希望能够使用内置机制做这样的事情:

@Url.Action("SearchWithQueryString","Search", new Test())

并得到与以前相同的结果。

有什么想法吗?

【问题讨论】:

标签: asp.net-mvc-4 razor


【解决方案1】:

您应该使用RouteValueDictionary 类。这允许您将模型转换为 QueryString:

@Url.Action("SearchWithQueryString", "Search", new RouteValueDictionary(new Test()))

例如,new Test() 也可以是 Model

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多