简体   繁体   中英

How to use fragment in Ajax.RouteLink?

I have a working Ajax.RouteLink, I want to add a fragment. 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:

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

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. You can't use Class , as it's a reserved word. The workaround is to prefix an @ , ie @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. So I took a look at the MSDN documentation on the extension method . Simply there's no overload that allows passing a fragment and anonymous objects for routeValues and htmlAttributes . If you want to use the fragment version(s) then you must pass RouteValueDictionary and IDictionary<string, object> for routeValues and htmlAttributes , respectively.

The issue you're having with RouteValueDictionary is that you need to actually initialize with dictionary members, so { "caseNo", caseNo } rather than caseNo = caseNo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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