【发布时间】:2013-01-18 00:05:05
【问题描述】:
Index.html(视图)
<div class="categories_content_container">
@Html.Action("_AddCategory", "Categories")
</div>
_AddCategory.cshtml(部分视图)
<script>
$(document).ready(function () {
$('input[type=submit]').click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: '@Url.Action("_AddCategory", "Categories")',
dataType: "json",
data: $('form').serialize(),
success: function (result) {
$(".categories_content_container").html(result);
},
error: function () {
}
});
});
});
</script>
@using (Html.BeginForm())
{
// form elements
}
控制器
[HttpPost]
public ActionResult _AddCategory(CategoriesViewModel viewModel)
{
if(//success)
{
// DbOperations...
return RedirectToAction("Categories");
}
else
{
// model state is not valid...
return PartialView(viewModel);
}
}
问题:如果操作成功,我希望重定向到另一个页面(类别)。但没有行动,没有错误信息。如果操作不成功,它就像我的预期一样工作。
我该怎么做?如何使用 AJAX 发布路由另一个页面?
【问题讨论】:
-
您是否已经尝试添加重定向?你的问题暗示是的,但我在你的代码中看不到这样的功能。
-
OIC,您正试图在您的操作中重定向
-
@DaveA,我的意思是控制器成功,而不是 ajax。我添加
return RedirectToAction("Categories");进行重定向。如果模型状态无效,则 ajax 发布成功(这是预期的)。我想在控制器动作中重定向,这可能吗?或者如果没有,我怎样才能在 ajax 成功中捕捉到这一点并重定向到另一个页面。总结,如果 DbOperations 成功重定向另一个页面,我想再次使用模型加载 partialView。
标签: javascript jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-4