user3624511 user3624511 - 1 year ago 69
jQuery Question

jQuery Call Controller's Action

Hi I have a Function that should call Controller Action once its called. But it is not calling Action Method; could you please advice

function timedCount() {
c = c + 1;
remaining_time = max_count - c;
if (remaining_time == 0 && logout) {
// This is I am trying to achieve
location.href = $('Home/Logout').val();
// window.location.href = ""; -- Works

} else {
t = setTimeout(function () { timedCount() }, 1000);

I am trying to achieve something like Logout Modal

Answer Source

To be safe, If your code is in a razor view, you should use the Url.Action Html helper to generate the correct relative path to the action method.

Assuming your code is inside the razor view,


If your code is inside an external js file (not inside the razor view), you can still generate the url using the helper methods and pass it to the javascript.

One way is to keep an html element in your razor view and read that using jQuery in your javascript code.

<input type="hidden" value="@Url.Action("Logout","Home")" id="logoutUrl" />

And in your javascript,


Another (better) solution is to pass a javascript setting object with the needed urls to javascript as explained in this post How do I make JS know about the application root?.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download