0

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();
    }
4
  • If your View containing the OnSuccess function is not rendered as part of the _LoginPartial partial view, than the value of @ViewBag.ReturnUrl will be null. Commented Sep 3, 2013 at 19:39
  • You are also not setting ViewBag.ReturnUrl in your _LoginPartial POST action method. Commented Sep 3, 2013 at 19:41
  • I think the JavaScript could use some semicolons as well. Commented Sep 3, 2013 at 21:28
  • @asymptoticFault I love how you're always there for me, man! I forgot to include the parent view controller, I'll post that up as well as that's where the URL is set. The returnurls in the partials are from old code and are not used. I figured if the parent view has the returnURL in there, the javascript postbacks would be able to use the scripts rendered in the parent view CustomScripts section. As for the semicolons, they always give me a syntax error when I throw them in. Commented Sep 4, 2013 at 17:17

1 Answer 1

1

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>

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.