asp.net mvc using ajax, but the callback won't redirect -
i'm using ajax 2 different partial views in same parent view. ajax request submitted successfully, , onsuccess() function called, page won't redirect. here's code. i've made sure returnurl 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 creative works account log in.</h2> @html.action("_loginpartial", new {returnurl = viewbag.returnurl }) } } </section> <section id ="registerform"> @{ if(!websecurity.isauthenticated){ <span>don't have account? make one!</span> @html.action("registerpartial", new { returnurl = viewbag.returnurl }) } } </section> @section scripts { @scripts.render("~/bundles/jqueryval") }
partialview 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 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 got far, failed, redisplay form modelstate.addmodelerror("", "the user name or password provided 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(); }
the problem me accessing viewbag directly in javascript. answer replace javascript this.
@section customscripts { <script type ="text/javascript"> function onsuccess() { var returnurl = @html.raw(json.encode(viewbag.returnurl)) window.location = returnurl; } </script>
}
Comments
Post a Comment