简体   繁体   English

如何在Ajax.RouteLink中使用片段?

[英]How to use fragment in Ajax.RouteLink?

I have a working Ajax.RouteLink, I want to add a fragment. 我有一个工作的Ajax.RouteLink,我想添加一个片段。 I can't figure out correct syntax. 我找不到正确的语法。

Working route without fragment: 工作路线无碎片:

return Ajax.RouteLink(
            linkText,
            routeName,
            new { caseNo = caseNo, controller = controllerName, option = contentOption },
            new AjaxOptions() { UpdateTargetId = "caseContent", HttpMethod = "Post", OnBegin = onBegin, OnSuccess = onSuccess },
            new { Class = cssClass + (contentOption != null && contentOption == contentView ? " active" : "") });

Compiler Error RouteLink with fragment: 带有片段的编译器错误RouteLink:

return Ajax.RouteLink(
            linkText: linkText,
            routeName: routeName,
            protocol: null,
            hostName: null,
            fragment: fragment,
            routeValues: new { caseNo = caseNo, controller = controllerName, option = contentOption },
            ajaxOptions: new AjaxOptions() { UpdateTargetId = "caseContent", HttpMethod = "Post", OnBegin = onBegin, OnSuccess = onSuccess },
            htmlAttributes: new { Class = cssClass + (contentOption != null && contentOption == contentView ? " active" : "") }
            );

Errors with routeValues and htmlAttributes. routeValues和htmlAttributes错误。 Cannot convert from anonymous type. 无法从匿名类型转换。

I tried: 我试过了:

routeValues: new System.Web.Routing.RouteValueDictionary() { caseNo = caseNo, controller = controllerName, option = contentOption }

but then the complier complains about my routing parms (caseNo, controller, option). 但是后来,编译器抱怨我的路由参数(caseNo,controller,option)。

Also, is it okay to leave protocol and hostname null, as I'm not changing those? 另外,可以将协议和主机名保留为空,因为我没有更改它们吗?

The only problem I see is your use of Class in htmlAttributes for both calls of Ajax.RouteLink you posted. 我看到的唯一问题是你使用的Class在htmlAttributes为两个电话Ajax.RouteLink您发布。 You can't use Class , as it's a reserved word. 您不能使用Class ,因为它是保留字。 The workaround is to prefix an @ , ie @class . 解决方法是给@ @class前缀,即@class

Sorry, you're right about that. 抱歉,您是对的。 I got tricked, I think, by the syntax highlighting. 我认为语法突出显示让我受骗。 Anyways, after taking another look, I found your problem. 无论如何,在再次查看之后,我发现了您的问题。 I don't use any of the Ajax.* helpers personally, so I'm not familiar with their signatures. 我个人不使用任何Ajax.*助手,因此我对它们的签名不熟悉。 So I took a look at the MSDN documentation on the extension method . 因此,我看了有关扩展方法的MSDN文档 Simply there's no overload that allows passing a fragment and anonymous objects for routeValues and htmlAttributes . 根本就没有重载,它允许为routeValueshtmlAttributes传递一个fragment 匿名对象。 If you want to use the fragment version(s) then you must pass RouteValueDictionary and IDictionary<string, object> for routeValues and htmlAttributes , respectively. 如果要使用fragment版本,则必须分别为routeValueshtmlAttributes传递RouteValueDictionaryIDictionary<string, object>

The issue you're having with RouteValueDictionary is that you need to actually initialize with dictionary members, so { "caseNo", caseNo } rather than caseNo = caseNo . RouteValueDictionary存在的问题是您实际上需要使用字典成员进行初始化,因此{ "caseNo", caseNo }而不是caseNo = caseNo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM