2014-09-13 34 views
0

我想將routeValues作爲複雜的數據類型傳遞給HtmlHelper,而不是使用匿名類型。MVC:從複雜數據類型中提取RouteValues

例如,

@Html.Navigator("Title", Model.SearchCriteria) 

,而不是

@Html.Navigator("Title", new {startDate=Model.StartDate, filter=Model.Filter, ...and so on}) 

對於後者我用:

public static MvcHtmlString Navigator(this HtmlHelper html, string title, object routeValues) 

anchorBuilder.MergeAttribute("href", url.Action(action, routeValues)); 

添加routeValues。

無法找到SearchCriteria的操作方法。 任何建議,指針..?

問候,蓋伊

回答

0

您可以基於對象創建一個新的RouteValuesDictionary。假設Model.SearchCriteriaSearchCriteria

public static MvcHtmlString Navigator(this HtmlHelper html, string title, SearchCriteria searchCriteria) 
{ 
    RouteValuesDictionary routeValues = new RouteValuesDictionary(searchCriteria);