Fitzpleasure
Fitzpleasure

Reputation: 109

asp.net mvc using ajax, but the callback won't redirect

I'm using ajax for two different partial views in the same parent view. The ajax request is submitted successfully, and the OnSuccess() function is called, but the page won't redirect. Here's my code. I've made sure that the returnurl is an absolute address. VIEW:

@{
ViewBag.Title = "Log in/Register";
}

<hgroup class="title">
<h1>@ViewBag.Title.</h1>
</hgroup>

@section CustomScripts {
<script type ="text/javascript">
    function OnSuccess(){
         var returnUrl = @ViewBag.ReturnUrl
         window.location =  returnUrl
    }
</script>
}

<section id="loginForm">

@{
if(!WebSecurity.IsAuthenticated){
    <h2>Use a Creative Works account to log in.</h2>
@Html.Action("_LoginPartial", new {returnUrl = ViewBag.ReturnUrl })

}
}
</section>
<section id ="RegisterForm">
@{
    if(!WebSecurity.IsAuthenticated){
        <span>Don't have an account? Make one!</span>
        @Html.Action("RegisterPartial", new { returnUrl = ViewBag.ReturnUrl })
    }
}
</section>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

PartialView for login:

@{
ViewBag.Title = "LoginPartial";
}
@using (Ajax.BeginForm("_LoginPartial", new {returnUrl = ViewBag.ReturnUrl}, 
new AjaxOptions(){UpdateTargetId = "loginForm", 
InsertionMode = InsertionMode.Replace, OnSuccess = "OnSuccess"
})) {        

 @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Log in Form</legend>
        <ol>
            <li>
                @Html.LabelFor(m => m.UserName)
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </li>
            <li>
                @Html.LabelFor(m => m.Password)
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </li>
            <li>
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
            </li>
        </ol>
        <input type="submit" value="Log in" />
    </fieldset>

    }

and the controller:

[AllowAnonymous]
    public ActionResult _LoginPartial(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return PartialView(new LoginModel());
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult _LoginPartial(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password,     persistCookie: model.RememberMe))
        {
            return PartialView();
        }

        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
        return PartialView(model);
    }

EDITS: Parent View Controller

[AllowAnonymous]
    [DisableCache]
    public ActionResult Login(string returnUrl ="/")
    {
        var path = VirtualPathUtility.ToAbsolute(returnUrl);
        var url = new Uri(Request.Url, path).AbsoluteUri;
        ViewBag.ReturnUrl = url;
        return View();
    }

Upvotes: 0

Views: 753

Answers (1)

Fitzpleasure
Fitzpleasure

Reputation: 109

The problem was me accessing viewbag directly in my javascript. The answer was to replace my javascript with this.

@section CustomScripts {
 <script type ="text/javascript">
     function OnSuccess() {
         var returnUrl = @Html.Raw(Json.Encode(ViewBag.ReturnUrl))
         window.location = returnUrl;
    }
</script>

}

Upvotes: 1

Related Questions