0

我有以下代码用于生成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>
4

1 回答 1

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
)

You have to choose one of them. That is send both parameters 'routeValues' and 'htmlAttributes' as anonymous object (1st one) or as typed object, 'RouteValueDictionary' for 'routeValues' and 'IDictionary<string, Object>' for 'htmlAttributes'. Your given code matched the 1st one, that is why the 'htmlAttributes' of type IDictionary<string, Object> treat as Object and generating the incorrect tag.

Correct code should be:

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

Or

@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}                                                                       
            })
于 2012-04-16T03:58:45.440 回答