user3624511
user3624511

Reputation: 27

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) {
            $('#logout_popup').modal('hide');
            // This is I am trying to achieve 
            location.href = $('Home/Logout').val();
           // window.location.href = "www.google.com/";  -- Works

        } else {
            $('#timer').html(remaining_time);
            t = setTimeout(function () { timedCount() }, 1000);
        }
    }

I am trying to achieve something like Logout Modal

Upvotes: 0

Views: 365

Answers (1)

Shyju
Shyju

Reputation: 218702

 location.href="Home/Logout";

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,

location.href="@Url.Action("Logout","Home")";

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,

location.href=$("#logoutUrl").val();

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

Upvotes: 1

Related Questions