2011-02-17 17 views
2

嘿SO ...這裏是一個謎:如何在傳遞Html屬性時使Html.RouteLink()與RouteValueDictionary一起使用?

<%: Html.RouteLink("asd",item.RouteValues) %> 
    <%: Html.RouteLink("asd",item.RouteValues, new{title="title"}) %> 

第一行正確地吐出了這樣的代碼:

<a href="/">asd</a> 

第二行不正確地吐出了這樣的代碼:

<a href="/?Count=4&amp;Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object%5D&amp;Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D" title="title">asd</a> 

我期待的代碼如下所示:

<a href="/" title="title">asd</a> 

於是,我就在視圖更明確一點寫這樣說:

<%: Html.RouteLink("asd",(RouteValueDictionary)item.RouteValues, new{title="title"}) %> 

但我有同樣的(不正確)的結果。

有什麼想法?

+0

當我看重載時,我意識到`object routValues`和`RouteValueDictionary`之間有區別 – quakkels 2011-02-17 18:06:57

回答

1

的問題是有沒有過載Html.RouteLink(string, RouteValueDictionary, object)

所以,我需要通過我的HTML attriute作爲這樣一個字典:

<%: Html.RouteLink(
     "link", 
     item.RouteValues, 
     new Dictionary<string,object>{{"title", "title text"}}) %> 

的凌亂類...但它的工作原理。

1

當您期待匿名對象時,您正在傳遞RouteValueDictionary。

相關問題