在您的控制器操作中,您可以返回 aPartialView
或JsonResult
指向控制器操作以重定向到:
public ActionResult SomeAction()
{
if (HasPassedValidation)
{
// everything went fine => let's return a partial view
// that will be updated
return PartialView();
}
// something went wrong with the validation =>
// we return a JsonResult pointing to the controller
// action we want to redirect to
var result = new
{
redirectTo = Url.Action("SomeOtherAction", "SomeController")
};
return Json(result, JsonRequestBehavior.AllowGet);
}
然后在你的 AJAX 调用测试的成功回调中,在这种情况下你是并采取相应的步骤:
success: function(result) {
if (result.redirectTo) {
// the controller action returned a JSON result => there was an error
// => let's redirect
window.location.href = result.redirectTo;
} else {
// everything went fine => let's update the DOM with the partial
$('#results').html(result);
}
}