Here's what I implemented some time ago, not like your script, but it can certainly help you forward:
My HTML markup:
<button id="btnCancel" type="button" class="t-button">Cancel</button>
With jQuery I can specify where it needs to go:
<script type="text/javascript"> $(document).ready(function () { $('#btnCancel').click(function () { window.location = '@Url.RouteUrl(Url.GrantApplicationIndex())'; }); }); </script>
My GrantApplicationIndex helper method code:
public static object GrantApplicationIndex(this UrlHelper instance) { Check.Argument.IsNotNull(instance, "instance"); return new { controller = "GrantApplication", action = "Index" }; }
When the page is displayed, the window.location part will look like this:
window.location = '/GrantApplication';
Never mix your HTML and JavaScript code, but rather subscribe to the event after loading the control in the DOM.
source share