此发布操作是从 ajax jquery 对话框中调用的。
当我选择模板时,它应该运行重定向到操作方法,但从未命中单元控制器和 GetTemplateRootUnits 操作?
我做错了什么?
[HttpPost]
public ActionResult Open(int selectedTemplateId)
{
if (ModelState.IsValid)
{
return RedirectToAction("GetTemplateRootUnits", "Unit", new { TemplateId = selectedTemplateId });
}
else
{
return LoadOpenTemplates();
}
}
我的路由表:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }
);
要调用的目标控制器/操作:
[HttpGet]
public JsonNetResult GetTemplateRootUnits(int templateId)
{
IEnumerable<Unit> units = _unitDataProvider.GetTemplateRootUnits(templateId);
return new JsonNetResult(new { data = units });
}
function openTemplate(dlg, form) {
$.ajax({
url: $(form).attr('action'),
type: 'POST',
data: form.serialize(),
success: function (response) {
if (response.success) {
dlg.dialog("close");
$('#TreeDiv').empty();
loadUnits(response.data);
}
else { // Reload the dialog with the form to show model/validation errors
dlg.html(response);
}
}
});
}
使用
AJAX POST 时RedirectToAction
不起作用看:
重定向到操作不起作用
在这种情况下,您需要使用 javascript 进行重定向。
有几种方法:
如何在 jQuery Ajax 调用后管理重定向请求
重定向到操作不重定向