Redirect to another view using Javascript


Hi, I have this script :

  function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me?fields=email,name', function (response) {
                console.log('Successful login for: ' + response.name);
                // should redirect logger to ExternalLogin
                //window.location.href = "/Account/ExternalLogin.cshtml";
                //document.getElementById('status').innerHTML =
                //    'Thanks for logging in, ' + response.name + '!';

            });

Should after that line : 

console.log('Successful login for: ' + response.name);
             

I will redirect to view Account/LoginExternal.cshtml.

I tried to add this code of line

window.location.href = "/Account/ExternalLogin.cshtml";

But it is not working for me. how can I do that. Thanks.


Asked by:- LuneAgile
0
: 5538 At:- 9/13/2018 4:18:08 PM
ASP.NET JavaScript







2 Answers
profileImage Answered by:- neena

You cannot call View directly, I think you need to clear your concepts of MVC first.

You need to call ActionMethod which will return a view, so it must be like

window.location.href = "/Controller/ActionMethod";

If an ActionMethod is ExternalLogin is in Account controller which returns ExternalLogin.cshtml, like below

        public ActionResult ExternalLogin()
        {
            // Some C# code
            return View()
        }

You can have JS to redirect

window.location.href = "/Account/ExternalLogin";

This is the correct way.

1
At:- 9/13/2018 5:56:52 PM


profileImage Answered by:- LuneAgile

Hi,  Neena Thanks a lot of for your answar.

0
At:- 9/13/2018 8:14:29 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use