user3683706
user3683706

Reputation: 103

MVC page to page redirection

I'm new to MVC (using 4, framework 4.0) and I understand the basics but this page redirect isn't working as I expect. The application is a login/authentication which if the user successfully logs in it redirects them to the target application. That part works just fine. However the user may forget his/her login credentials, so I have a series of pages that will prompt the user for a registered email address and decoded captcha value. If that information is validated then another page prompts for a series of (up to 3) pre-determined security question answers (in the case of a password forgotten). If the security challenge question is successfully answered the user is redirected to a password change page. At any point in the process the user may click a cancel button which should redirect back to the login page and clear any state variables tracking their progress through the recovery process. The problem is I keep getting stuck on pages that even after a RedirectToAction("SomeAction", "SomeController"); I still stay on the page? The URI even changes on the browser but the page asking for email address or security question stays active. I'm using an ajax $.get() to call various actions for submit and cancel.

view is defined like this:

@using (Html.BeginForm("RecoverUserCredentialsByModel", "Account", FormMethod.Get, new { id = "form1" }))
{
<!--... three input controls and a submit and cancel button-->
<p>
        <button id="btnSubmit" onclick="return CheckUserEmail()">Submit</button>
        <button id="btnCancel" onclick="return CancelRecovery()">Cancel</button>
</p>
}

<script type="text/javascript">
        function CheckUserEmail() {
            var emailAddress = document.getElementById("EmailAddress").value;
            var pictogramHref = document.getElementById("pictogramHref").src;
            var pictogramAnswer = document.getElementById("Pictogram").value;
            if (emailAddress != null) {
                var url = "/Account/ValidateEmail"; 
                $.get(url, { "emailAddress": emailAddress, "pictogramHref": pictogramHref, "pictogramTranslation": pictogramAnswer }, null);
                return true;
            }
        }
</script>

<script type="text/javascript">
    function CancelRecovery() {
        var url = "/AuthenticationModule/Account/CancelRecovery";
        $.get(url, {}, null);
        return true;
    }
</script>

Codebehind redirections look like:

/// <summary>
/// Global cancel recovery, clears the stateful session object and redirects back to login view
/// </summary>
/// <returns>ActionResult</returns>
[AllowAnonymous]
public ActionResult CancelRecovery()
{
        LoginModel statefulLoginModel = null;
        try
        {
            // Reset everything active and redirect to login view
            statefulLoginModel = new LoginModel();
            Session["LoginModel"] = statefulLoginModel;
            return Redirector(statefulLoginModel);
        }
        catch (Exception ex)
        {
            // Log the error and Reset everything active and redirect to login view
            FileLogger.Log(ex);
            statefulLoginModel = new LoginModel();
            Session["LoginModel"] = statefulLoginModel;
            return Redirector(statefulLoginModel);
        }
}

[AllowAnonymous]
public ActionResult Redirector(LoginModel model)
{
... some code
            Session["LoginModel"] = statefulLoginModel;
            if (loginState == 0)
            {
                RedirectToAction("LogOn");
            }

}

When it hits the RedirectToAction("LogOn"); the view "RecoverUserInfo" stays active on the browser and no redirection occurs?

What am I doing wrong?

Upvotes: 2

Views: 1799

Answers (1)

Amit
Amit

Reputation: 823

Try this..........

Proper Syntax is return RedirectToAction("ActionName","ControllerName");

In this case if Logon Action is written on the same Controller Then use following Code..

return RedirectToAction("LogOn");

or it is written on another controller then just replace your Action Name and Controller Name in the following code.

return RedirectToAction("ActionName","ControllerName");

Upvotes: 1

Related Questions