ASP.NET MVC - How to get Ajax 'onclick' for a specific action (I need for Ajax.ActionLink what Url.RouteUrl does for Html.ActionLink)
-
05-09-2019 - |
Question
REVISION
I want add a onclick to a element that handles a Ajax request, not just a route-redirect.
I have these options for a route-url.
Html.ActionLink(params)
Url.RouteUrl(params) 'returns JUST ActionUrl
So I can go like
<div onclick="javascript:location.href('<%=Url.RouteUrl(params)%>')"></div>
But how do I generate the following??
<div onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'WorkorderDetails' });"></div>
Solution
It turns out that it is not possible. The Sys.Mvc.AsyncHyperlink.handleClick() function relies on the anchor tag to function correctly. The href specified in the anchor is used to determine which service is called on the server.
I ended up creating a Ajax.ActionLink() and making it invisible (display:none). I know invoke the click using Javascript. jQuery actually (normal invoke: link.click() does not work properly!):
$(link).trigger("click");
OTHER TIPS
I'm not sure if I understand the question right, but you want just the raw URL, based upon an Action Method or something?
Then how about the UrlHelper class?
This previous question on SO discusses this option.
Like I said, I'm not sure I understood the question 100%, though.