将属性传递给ActionLink

时间:2012-04-15 14:21:19

标签: asp.net-mvc

我有以下代码用于生成a标记:

<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>

哪里出错?
感谢。

更新 我想要以下内容:

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

1 个答案:

答案 0 :(得分: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说是'session','hall','schedule'是queryStirng参数,那么代码应该是:

@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)

否则,如果你想要'session','hall','schedule'作为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'作为匿名对象(第一个)或作为类型对象,'routeValueDictionary'表示'routeValues','IDictionary<string, Object>'表示'htmlAttributes'。您给定的代码与第一个匹配,这就是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}                                                                       
            })