2013-11-26 73 views
2

我正在尋找一種方法來將mvc4模型轉換爲查詢字符串。 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()) 

,並得到相同的結果,我先前。

任何想法?

+1

你檢查了嗎? http://stackoverflow.com/questions/9817591/convert-querystring-from-to-object –

+0

這是一個微不足道的解決方案。反射。 我想使用內置機制來避免創建新代碼。 – Dvir

回答

3

您應該使用RouteValueDictionary類。這可以讓你的模型轉換爲查詢字符串:

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

new Test()也可能是Model例如。

相關問題