2012-04-15 84 views
0

我有下面的代碼生成a標籤:傳球屬性ActionLink的

<ul> 
    @foreach (var schedule in scheduleGroup) 
    { 
     <li> 
     @Html.ActionLink(string.Format("{0:HH\\:mm}", schedule.RecurrenceStart), "EventOverview", "BaseEvent", new { id = schedule.BaseEvent.OID, type = schedule.BaseEvent.XPObjectType}, new Dictionary<string, object> 
       { 
        {"session", schedule.SessionId}, 
        {"hall",schedule.HallId}, 
        {"client",schedule.BasePlace.PremieraClientId}                  
       }) 
    } 
</ul> 

但是,HTML屬性顯示不正常的a標籤。這是產生的標記:

<a href="/Film/Event/36" values="System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object]" keys="System.Collections.Generic.Dictionary`2+KeyCollection[System.String,System.Object]" count="3" comparer="System.Collections.Generic.GenericEqualityComparer`1[System.String]">20:15</a> 

哪裏出錯?
謝謝。

UPDATE: 我想以下幾點:

<a client="1" hall="1" session="15" href="/BaseEvent/EventOverview?id=36&type=Film"> 20:15 </a> 
+0

你能告訴我們什麼結果你希望它是什麼? – Silvermind 2012-04-15 17:49:34

+0

我想他想要生成與會話,大廳和客戶端變量設置 - > Tommy 2012-04-15 18:20:54

+0

@Tommy:對於延遲答案抱歉。我更新了問題。請看。 – user348173 2012-04-16 03:58:02

回答

1

您的代碼:

@Html.ActionLink(string.Format("{0:HH\\:mm}", schedule.RecurrenceStart), "EventOverview", "BaseEvent", 
       new { id = schedule.BaseEvent.OID, type = schedule.BaseEvent.XPObjectType}, 
       new Dictionary<string, object> 
           { 
            {"session", schedule.SessionId}, 
            {"hall",schedule.HallId}, 
            {"client",schedule.BasePlace.PremieraClientId}                  
           }) 

如果你的目的是要產生一個鏈接作爲@Tommy說,這就是 '會話',「大廳「,」時間表「均爲查詢參數,則代碼應爲:

@Html.ActionLink(string.Format("{0:HH\\:mm}", "schedule.RecurrenceStart"), "EventOverview", "BaseEvent", 
     new { id = schedule.BaseEvent.OID, type = schedule.BaseEvent.XPObjectType, 
       session= schedule.SessionId, hall =schedule.HallId, client =schedule.BasePlace.PremieraClientId}, 
     null) 

否則,如果你想在「會話」,「館」,「時間表」作爲HTML屬性( 根據你定的代碼),有兩個匹配ActionLink方法簽名:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 

而且

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    RouteValueDictionary routeValues, 
    IDictionary<string, Object> htmlAttributes 
) 

你必須選擇其中之一。即發送參數'routeValues'和'htmlAttributes'作爲匿名對象(第一個),或者作爲'routeValues'的類型對象'RouteValueDictionary','htmlAttributes'發送'IDictionary<string, Object>'。您的代碼與第一個代碼匹配,這就是爲什麼IDictionary<string, Object>類型的'htmlAttributes'被視爲Object並生成不正確的代碼的原因。

正確的代碼應該是:

@Html.ActionLink(linkText: string.Format("{0:HH\\:mm}", "schedule.RecurrenceStart"), actionName: "EventOverview", controllerName: "BaseEvent", 
     routeValues: new { id = schedule.BaseEvent.OID, type = schedule.BaseEvent.XPObjectType }, 
     htmlAttributes: new 
     { 
      session = schedule.SessionId, 
      hall = schedule.HallId, 
      client = schedule.BasePlace.PremieraClientId 
     }) 

或者

@Html.ActionLink(string.Format("{0:HH\\:mm}", "schedule.RecurrenceStart"), "EventOverview", "BaseEvent", 

     new RouteValueDictionary(new { id = schedule.BaseEvent.OID, type = schedule.BaseEvent.XPObjectType }), 

     new Dictionary<string, object> 
      { 
       {"session", schedule.SessionId}, 
       {"hall", schedule.HallId}, 
       {"client", schedule.BasePlace.PremieraClientId}                  
      }) 
+0

很酷,謝謝。這是我需要的(第二種選擇)。謝謝。 – user348173 2012-04-16 04:08:20