1 回答
TA贡献1828条经验 获得超6个赞
AJAX 回调不打算执行重定向到另一个操作方法(也data使用字符串连接分配参数data: '{ID: "' + param + '" }'肯定是错误的)。尝试配置 AJAX 回调以查看错误消息是否存在,如下所示:
$.ajax({
type: "GET",
url: '@Url.Action("_SampleAction", "SampleController")',
data: { ID: param }, // proper way to pass string to controller with AJAX
// other AJAX settings
success: function (result) {
if (result.message == undefined) {
$('#myModelContent').html(result);
$("#myModal").modal('show');
} else {
// set redirection here instead of using RedirectToAction from controller
window.location.href = result.url;
}
},
error: function (req, status, error) {
alert("Error occurred");
}
});
});
并且控制器动作应该在异常发生时返回 JSON 数据,如下所示:
public ActionResult _SampleAction(string ID)
{
MyModel objMyModel = new MyModel();
objMyModel.ID = ID;
try
{
// do something
return PartialView("_SampleAction", objMyModel);
}
catch (Exception ex)
{
return Json(new { url = Url.Action("Index", "Error"), message = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
- 1 回答
- 0 关注
- 195 浏览
添加回答
举报