asp.net mvc使用ajax,但回调不会重定向

时间:2022-11-06 18:49:00

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:

我在同一父视图中使用ajax用于两个不同的局部视图。成功提交了ajax请求,并调用了OnSuccess()函数,但页面不会重定向。这是我的代码。我已经确定returnurl是一个绝对的地址。视图:

@{
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:

用于登录的PartialView:

@{
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();
    }

1 个解决方案

#1


1  

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

问题是我直接在我的javascript中访问viewbag。答案是用这个替换我的javascript。

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

}

}

#1


1  

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

问题是我直接在我的javascript中访问viewbag。答案是用这个替换我的javascript。

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

}

}