user1483145
user1483145

Reputation: 129

Call MVC Action from Jquery

Is it possible to an call MVC action(Logout) from the jquery setTimeout function?

I have tried the following code:

setTimeout(function () { @Html.Action("Logout") }, 150000);

Upvotes: 0

Views: 340

Answers (1)

Erik Philips
Erik Philips

Reputation: 54618

Do you understand the difference between when code runs?

This code:

setTimeout(function () { @Html.Action("Logout") }, 150000);

Will produce something like this on the client:

 setTimeout(function () { <div><a href="">Logout</a></div> }, 150000);

Which is completely invalid javascript (regardless what it actually does it will return html normally).

You could do (I think this is right)

 setTimeout(function () 
   { 
     window.location = '@Url.Action("Logout","Account")'; 
   }, 150000);

Which would produce something like:

 setTimeout(function () 
   { 
     window.location = '/Account/Logout'; 
   }, 150000);

Upvotes: 2

Related Questions