How to Get an ASP.NET MVC Ajax Response to Redirect to New Page Instead of Inserting View into Updatetargetid

How to get an ASP.NET MVC Ajax response to redirect to new page instead of inserting view into UpdateTargetId?

You can use JavascriptResult to achieve this.

To redirect:

return JavaScript("window.location = 'http://www.google.co.uk'");

To reload the current page:

return JavaScript("location.reload(true)");

Seems the simplest option.

Ajax Redirect to Page instead of Updating Target

To perform a redirect you need to do it on the client side. So you can no longer use UpdateTargetId but you should instead use the OnSuccess option. You will also need to modify the Logon controller action so that in case of a redirect you test if you it is an ajax request and in this case return a Json object with the redirect url which will be used in javascript:

if (ModelState.IsValid)
{
if (string.IsNullOrEmpty(returnUrl))
{
returnUrl = Url.Action("Index", "App");
}
if (Request.IsAjaxRequest())
{
return Json(new { returnUrl = returnUrl });
}
return Redirect(returnUrl);
}

And in the view:

<% using (Ajax.BeginForm(
"LogOn",
null,
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = "success"
},
new {
id = "SignInForm", ReturnUrl = Request.QueryString["ReturnUrl"]
})) { %>
<<Page HTML Controls>>
<input type="submit" value="Log On" />
<% } %>

<script type="text/javascript">
function success(context) {
var returnUrl = context.get_data().returnUrl;
if (returnUrl) {
window.location.href = returnUrl;
} else {
// TODO: update the target form element with the returned partial html
}
}
</script>

Redirect instead update Ajax MVC

Missing some scripts...

<script src="@Url.Content("~/Assets/Js/jquery.min.js")"></script>
<script src="@Url.Content("~/Assets/Js/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Assets/Js/jquery.validate.unobtrusive.min.js")"></script>
<script src="@Url.Content("~/Assets/Js/jquery.unobtrusive-ajax.min.js")"></script>

Ajax.BeginForm that can redirect to a new page

You could use JSON and perform the redirect on the client:

[HttpPost]
public ActionResult UpdateModel(BasketModel model)
{
if (model.Checkout)
{
// return to the client the url to redirect to
return Json(new { url = Url.Action("Checkout") });
}
else
{
return PartialView("_Updated");
}
}

and then:

@using (Ajax.BeginForm("UpdateModel", "MyController", new AjaxOptions { OnSuccess = "onSuccess", UpdateTargetId = "foo" }))
{
...
}

and finally:

var onSuccess = function(result) {
if (result.url) {
// if the server returned a JSON object containing an url
// property we redirect the browser to that url
window.location.href = result.url;
}
}

Ajax.BeginForm routing to new page instead of a partial view

The jquery-unobtrusive js file has to be included in your page to make the ajax.beginform work

Server side redirection after jquery ajax call in asp.net mvc 4

return usually returns from method without executing any further statements in it so else part is not needed. This way you will get rid of a problem #1.

As for redirecting why not return some kind of redirect command:

[HttpPost]
public ActionResult Index(LoginModel loginData)
{
if (login fails)
{
return Json(new {result = "InvalidLogin"}, JsonRequestBehavior.AllowGet);
}
return Json(new {result = "Redirect", url = Url.Action("MyAction", "MyController")});
}

And then in javascript:

$.post(url, data, function (response) {
if (response.result == 'InvalidLogin') {
//show invalid login
}
else if (response.result == 'Error') {
//show error
}
else if (response.result == 'Redirect'){
//redirecting to main page from here for the time being.
window.location = response.url;
}
});

RedirectToAction with Ajax.Beginform , unexpected results

Return the url you want to make a redirect to from the action method when the operation succeeded:

public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{
try
{
...
return Json(new { RedirectUrl = Url.Action("Details", new { id = s.GeneralSwitchTo }) });
}
...
}

And in the createsuccess:

function createsuccess(data) {
if (data.RedirectUrl)
window.location.href = data.RedirectUrl;
}

Ajax.BeginForm refreshes the view instead of updating target id

Make sure you have added the following script to your page:

  • jquery.unobtrusive-ajax.js

In ASP.NET MVC 3, jQuery is the default client side framework used for client side validation and Ajax.

Ajax helpers such as Ajax.BeginForm and Ajax.ActionLink emit HTML5 data-* attributes that are interpreted by the jquery.unobtrusive-ajax.js script and AJAXified in this way.

Also make sure that you have removed all references to Microsoft*.js scripts. Those are obsolete and should no longer be used in ASP.NET MVC 3.



Related Topics



Leave a reply



Submit