Javascript – Return JavaScript(). 4 good actions – 1 evil action !

asp.net-mvcjavascript

The bad 'return JavaScript' goes like this:

  1. This is the action link that gets selected.

    Ajax.ActionLink("Sign Out", "LogOff", "Account", new AjaxOptions { })
    
  2. This is the action.

    public ActionResult LogOff()   
    {      
        FormsAuth.SignOut();   
        return JavaScript("ClearDisplayName()");  
    } 
    
  3. The JavaScript is never called !

Additioinal Info:

All javascript functions are all in the .js file.

Four other actions, in the same file, do their return JavaScript(...) successfully.

I tested the four working actions by doing a return JavaScript("ClearDisplayName()") and
they all call ClearDisplayName() successfully.

I tested the failing action by doing a return JavaScript("OtherKnownWorkingJava()") with no luck.

Any idea's for this weird behavior ?

I noticed that all the successful actions pass through a View first.
The troubled action does not, it comes directly from an ActionLink.

Best Answer

After throwing some mud against this problem here's how I was able to call 'return JavaScript("ClearDisplayName")'.

Instead of trying to do 'return JavaScript("ClearDisplayName") from the LogOff action, I redirected to another action, LogOffA, and did a 'return JavaScript("ClearDisplayName") there, and it worked !!

    public ActionResult LogOffA()
    {
        return JavaScript("ClearDisplayName()");
    }

    public ActionResult LogOff()
    {
        FormsAuth.SignOut();
        return RedirectToAction("LogOffA", "Account");
        //return JavaScript("ClearDisplayName()");
    }