Return JavaScript (). 4 good actions - 1 atrocity!
Bad "reverse JavaScript" looks like this:
-
This is the action link that is selected.
Ajax.ActionLink("Sign Out", "LogOff", "Account", new AjaxOptions { })
-
This is the action.
public ActionResult LogOff() { FormsAuth.SignOut(); return JavaScript("ClearDisplayName()"); }
-
JavaScript is never called!
Additional Information:
All javascript functions are in the .js file.
Four other actions in the same file complete successfully return JavaScript(...)
.
I have verified four work steps by doing return JavaScript("ClearDisplayName()")
and they all call successfully ClearDisplayName()
.
I tested the failed action by running return JavaScript("OtherKnownWorkingJava()")
with no luck.
Any idea for this strange behavior?
I noticed that all successful actions go through the presentation first. The problematic action does not happen, it comes directly from ActionLink.
source to share
After picking some dirt against this problem, how was I able to call 'return JavaScript ("ClearDisplayName")'.
Instead of trying to execute 'return JavaScript ("ClearDisplayName") from the LogOff action, I redirected to another action, LogOffA , and there I did "return JavaScript (" ClearDisplayName ") and it worked!
public ActionResult LogOffA()
{
return JavaScript("ClearDisplayName()");
}
public ActionResult LogOff()
{
FormsAuth.SignOut();
return RedirectToAction("LogOffA", "Account");
//return JavaScript("ClearDisplayName()");
}
source to share
Are other actions also called by Ajax.ActionLink?
I doubt Ajax.ActionLink will handle JavaScript returns. If so, you either process the result yourself (using eval (response)), or avoid using Ajax.ActionLink, or instead return $ (Document) .ready (function () {<% = ViewData ["jsfunc"]%> };); - this will most likely be handled, although you will need jQuery.
source to share