0

I want to call a javascript function from within a controller function that I use:

 Public Function redirectTo() As JavaScriptResult
        Return JavaScript("ToSignUp()")
    End Function

in my controller. But the program never goes to the script. I've already checked similar answers but I haven't found any solution to the problem. Can someone assist me with this?

ADDITION 4/3/19 12:41
I use the following for redirection from my controller... but nothing is happening:

Public Function redirectTo() As RedirectToRouteResult
        Dim routes As New RouteCollection With {.RouteExistingFiles = True}
        Return RedirectToAction("../login/SignUp")
    End Function
End Class

ADDITION 4/3/19 23:20
The issue was solved by this way
In the code behind of my .aspx Page and at the proper place I add it the following code:

 Dim routes As New RouteCollection With {.RouteExistingFiles = True}
  Response.Redirect("SignUp")

The Response.Redirect instruction is not new.
But in order to be functional it needs to add before the following instruction

Dim routes As New RouteCollection With {.RouteExistingFiles = True}

And that is because the MVC did not recognize the existent files which means the property RouteExistingFiles is always False
Thus in order to work the code we need to turn this property to True
Anyway thanks to all for your assistance.

Lefteris Gkinis
  • 1,229
  • 6
  • 26
  • 63
  • 1
    You can't "call" javascript form the server they run in totally separate spheres. You can write some output to your view that will trigger javascript once the vire loads. I don't know vb.net but I'd imagine `Javascript` actually returns JSON which is a information exchange format. It's not executed – Liam Mar 04 '19 at 10:15
  • This question can't be answered without some additional information on when and how you want your JS to fire, etc. – Liam Mar 04 '19 at 10:17
  • Well the main Idea doing that was to redirect from the page that I work, into another page, which is in the same directory. – Lefteris Gkinis Mar 04 '19 at 10:18
  • 1
    So why don't you just return a `Redirect`? – Liam Mar 04 '19 at 10:18
  • What exactly mean by that? RedirectToAction?? Or?? – Lefteris Gkinis Mar 04 '19 at 10:19
  • 2
    Redirecting to another page has nothing to do with javascript :) Just use Redirect or RedirectToAction and provide it with url (a path to the destination page). – Lech Osiński Mar 04 '19 at 10:22
  • Possible duplicate of [ASP.Net MVC Redirect To A Different View](https://stackoverflow.com/questions/546461/asp-net-mvc-redirect-to-a-different-view) – Liam Mar 04 '19 at 10:25
  • 1
    "the main Idea doing that was to redirect from the page that I work, into another page"...agreed, JavaScript is definitely not needed for this. – ADyson Mar 04 '19 at 10:32
  • Please have a look in my addition – Lefteris Gkinis Mar 04 '19 at 10:43
  • 1
    How do you call this action? `Redirect` just issues a HTTP 302. If your using ajax or something then this won't be honoured by the calling code. – Liam Mar 04 '19 at 10:51
  • From my codebehind using `Attributes.loginPageController.redirectTo()`. In an other case I use `ajax` and works fine, that was the reason I want to use `javascript` – Lefteris Gkinis Mar 04 '19 at 10:53
  • I don't really know what your asking anymore. Please create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your problem – Liam Mar 04 '19 at 11:11
  • Yes I'll delete this an re-post it – Lefteris Gkinis Mar 04 '19 at 11:16

1 Answers1

1

From other examples online (like this one), I would say that if you temporarily changed your function to:

Public Function redirectTo() As JavaScriptResult
    Return JavaScript("alert("HERE");")
End Function

It will likely work... so without seeing the JS function contents, it's hard to tell. To the second point, a RedirectToAction call will work if called from the server; if the client is calling this, use 'window.location' instead.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • I Use ` Public Function callScript() As JavaScriptResult Return JavaScript("alert('ToSignUp()');") End Function` In order to call the script. But didn't work, did not pass thru the script... any suggestion? – Lefteris Gkinis Mar 04 '19 at 19:03
  • Are you sure the routing is occurring correctly then? If you open up the network tab, is it returning 404 possibly? – Brian Mains Mar 04 '19 at 22:45
  • Are you referring in my solution or in my notice above? And what did you mean 404? the error or something? – Lefteris Gkinis Mar 05 '19 at 07:15
  • I'm referring to whatever is requesting the "redirectTo" action; if it's not routing correctly, it could yield a 404 or 500 error, for instance... Using the debugging tools would help identify that. – Brian Mains Mar 05 '19 at 12:16
  • Yes.. OK at first this error was threw from the `redirectToRoute` but from the moment I add the `RouteExistingFiles` property to true then the error stop but nothing happen (I mean there was never redirect to anywhere) so I stop use the route (in general). – Lefteris Gkinis Mar 06 '19 at 16:06
  • Now about my routing table I have (except of the default) this record as well `routes.MapRoute( name:="SignUp", url:="{controller}/{action}/{id}", defaults:=New With {.controller = "login", .action = "SignUp", .id = UrlParameter.Optional} )` But except from the first record nothing else it works. Or rather I don't know how to make it work – Lefteris Gkinis Mar 06 '19 at 16:10