2012-02-08 206 views
19

我試圖附加屬性數據圖標添加到我的行動鏈接聲明,但我發現了以下錯誤:無效的匿名類型成員聲明。匿名類型的成員必須有一個成員賦值,簡單名稱或成員訪問

無效的匿名類型成員聲明。匿名類型成員必須使用成員分配,簡單名稱或成員訪問來聲明 。

作品:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
      new { @rel = "external", @id = "btnProfile" }) 

例外:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 }, 
      new { @rel = "external", @id = "btnProfile", @data-icon = "gear" }) 
+8

,而不是'@數據icon'使用'@ data_icon' – xandercoded 2012-02-08 16:01:27

+0

給出了同樣的異常 – Eugene 2012-02-08 16:13:42

回答

24

UPDATE:從上面Xander的註釋,使用data_icon = "gear"

呦u能到位HTML匿名對象的使用IDictionary<string, object>屬性:

@Html.ActionLink("Profile", "Details", "Profile", new { id = 11 } 
    , new Dictionary<string, object> 
    { 
     { "rel", "external" }, 
     { "id", "btnProfile" }, 
     { "data-icon", "gear" }, 
    }) 

看到這個過載:http://msdn.microsoft.com/en-us/library/dd504988.aspx

您所使用的助手是創建詞典的一種簡便方法,但幕後無論如何,該字典是創建。

+3

當我做到這一點,它呈現像鏈接: 'Log Off' – xdumaine 2012-08-30 22:04:46

+0

我有這個問題,在我的參數(一個類過濾器與我想通過一個成員的期間/點),我發現以下工作... '@ Html.ActionLink(「Profile」,「詳細信息「,新的RouteValueDictionary {{」filter.member「,myvalue.ToString()}})' 希望格式化工作 - 基本上使用RouteValueDictionary而不是匿名類型? – KeithFearnley 2018-02-28 20:39:12

3

我只是用下面

@using System.Web.Routing 

@{ 
    RouteValueDictionary RouteValues = new RouteValueDictionary(); 

    RouteValues["id"] = 11; 
    RouteValues[Some_Name] = Some_Value; //do this with as many name/value pairs 
             //as you like 
} 

@Html.ActionLink("Link Text", "Action", "Controller", RouteValues) 

這是我從喬恩的答案this post教訓。

我主要用這個在我的控制器爲RedirectToAction()方法提供的路由值,但我不明白爲什麼它不應該在你的視圖中工作,你將需要添加一個@using System.Web.Routing;雖然。

16

我想你使用下劃線像data_icon,並將其轉換它