我将 MVC3 (Razor) 与 Paging open source code here 一起使用.按下具有此代码的 Controller 的按钮时调用的代码:
[Authorize(Roles = SystemConstants.ROLE_ADMINISTRATOR)]
public ActionResult ListUsers(int? page)
{
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
var products = userToDisplay.ToPagedList(currentPageIndex, 5);
return PartialView("ListUsersTable", products);
}
这应该返回一个名为“ListUsersTable”的 PartialView。它确实返回“ListUsersTable”,但作为整个页面而不是替换 DIV。
这是 View 中的代码:
<div id="listUserToBeUpdated">
@Html.Partial("ListUsersTable", Model)
</div>
ListUsersTable 中执行 Ajax 调用的按钮如下所示:
<div class="pager">
@Ajax.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, "ListUsers", new AjaxOptions { UpdateTargetId = "listUserToBeUpdated" })
</div>
知道为什么代码没有替换 DIV 而是返回页面中的代码吗?
最佳答案
你引用了哪些JS文件?在 ASP.NET MVC3 中,他们将 AJAX 功能从 MicrosoftMvcAjax.js 更改为 jquery.unobtrusive-ajax.js,因此如果您还没有这样做,请引用后者。
还要验证 web.config 文件中的键“UnobtrusiveJavaScriptEnabled”是否设置为“true”
关于c# - Asp Mvc 使用 Ajax 返回局部 View 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032152/