此後期操作是從ajax jquery對話框調用的。RedirectToAction不會調用目標控制器和動作
當我有一個模板被選中時,它應該運行RedirectToAction方法,但是UnitController和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 }
);
我的目標控制器/行動呼籲:
:
當您使用AJAX POST 見[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);
}
}
});
}
兩件事情來檢查:首先,是實際的ModelState有效?其次,如果是,你的路由表是什麼樣的? – 2012-08-10 14:34:58
是的,否則代碼不會隱藏RedirectToAction。我使用asp.net mvc 4的默認路由表。 – Elisabeth 2012-08-10 15:08:42
什麼是Open返回? – 2012-08-10 15:22:31