【发布时间】:2016-03-26 08:12:47
【问题描述】:
在 View 我有一个链接按钮,并且有 java 脚本从视图中收集信息,然后发布到相应的操作 'GroupDeny'
@Html.ActionLink("Deny Selected", "GroupDeny", null, new { @class = "denySelectedLink" })
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).on('click', '.denySelectedLink', function (e) {
//Cancel original submission
e.preventDefault();
var identifiers = new Array();
//build the identifiers . . .
var jsonArg = JSON.stringify(identifiers);
$.post('/LicenseAssignment/GroupDeny?licensePermissionIdentifiers=' + encodeURIComponent(jsonArg));
});
</script>
然后在控制器中,GroupDeny 将更新数据库,然后 调用 RedirecToAction 以刷新视图
public class LicenseAssignmentController : Controller
{
[HttpPost]
public ActionResult GroupDeny(string licensePermissionIdentifiers)
{
// changes the DB
return RedirectToAction("Index");
}
// GET:
public async Task<ActionResult> Index()
{
var model = get data from DB
return View(model);
}
一切似乎都按预期工作,RedirectToAction("Index") 执行后将调用 Index,并且模型更新为我观看时的日期它在调试过程中,唯一的问题是页面根本没有刷新,也就是说视图仍然保持不变,但是在我手动刷新页面后(按F5),数据将使用DB中的值进行更新
【问题讨论】:
-
AJAX 不会遵循 302 重定向。如果您想重定向使用来自 post 的标准而不是 AJAX。或者您需要捕获响应并使用 JavaScript 导航。
-
@Jasen 感谢您的回复,但我是网络开发新手,请您提供更详细的信息吗?