c# - 使用 X.PagedList 在 ASP.NET Core 中更改页面时如何进行 Ajax?
问题描述
这是我的 ViewComponet:
@model X.PagedList.PagedList<CbWebApp.DTOs.UsuarioDTO>
@using X.PagedList.Mvc.Core
@using X.PagedList.Mvc.Common
//.....more code
<div class="pagination-sm text-center">
Página @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) de
@Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("ListaUsuario", new { page = page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions { Display = PagedListDisplayMode.IfNeeded, MaximumPageNumbersToDisplay = 5 }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "Get", UpdateTargetId = "usuariosPartial" }))
</div>
// more code....
我的 partialView 具有要呈现的 ViewComponent 和引用的 DIV:
<div id="usuariosPartial" class="col-xs-12 col-md-12">
@await Component.InvokeAsync("Usuario")
</div>
也许我可以获得 JQuery 的 X.PagedList HTML id,例如:
// is this the correct id?
$("#pagesizelist").change(function (event) {
我尝试了该 ID,但没有成功。:(
解决方案
好吧,我找到了这个解决方案,但我不确定是否是最好的方法:
JQuery - 为您的表格 tr 标签提供一个 ID,并在 jQuery 中的“a”标签的点击事件中引用它,如下所示:
$('#replaceMyTr').on('click', 'a', function (e) { e.preventDefault(); $("#icon").hide(); $("#progress").show(); $("#msg").hide(); $('input, button, a').disable(true); var IdDoPerfilDoUsuario; var este = $(this); function getUrlVars() { var vars = [], hash; var hashes = este.attr("href").slice(este.attr("href").indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } var page = getUrlVars()["page"]; if ($("select option:selected").first().val() === "--Todos--") { IdDoPerfilDoUsuario = 0; } else { IdDoPerfilDoUsuario = $("select option:selected").first().val(); } $("#usuariosPartial").hide(); este.attr('disabled', 'disabled'); $.ajax({ url: "/Usuario/ListaUsuario", type: 'GET', cache: false, data: { IdDoPerfilDoUsuario: IdDoPerfilDoUsuario, page: page }, success: function (result) { $("#icon").show(); $("#progress").hide(); $("#msg").show(); $('input, button, a').disable(false); $("#usuariosPartial").show(); $('#usuariosPartial').html(result); } }); return false; });
查看代码:
// more code... <tfoot> <tr id="replaceMyTr"> @*<td colspan="7">*@ <td> <div class="pagination-sm text-center"> @Html.PagedListPager((IPagedList)Model.Usuarios.ToEnumerable(), page => Url.Action("ListaUsuario", new { page = page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new PagedListRenderOptions { Display = PagedListDisplayMode.IfNeeded, MaximumPageNumbersToDisplay = 5 }, new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "teste" })) </div> </td> </tr> </tfoot> //....more code
推荐阅读
- c# - 从 JavaScript 调用 .razor 页面中的实例方法
- javascript - 基于正则表达式的行删除脚本(JS)不起作用
- java - 有没有办法让 java.awt.Choice 进一步下拉?
- android - Reliance JIO 网络中很少有 Android 应用程序无法/突然无法运行
- python - Scrapy Splash AttributeError:“HtmlResponse”对象没有属性“数据”
- mysql - 如何在时间到达和离开列之间选择数据?
- php - 如何修复我的表单没有将数据更新到数据库 [Codeigniter]
- sql - 在 SQL 中添加重复记录的值并在报表中显示
- python - 无法使用 Pillow 和 Python 居中字符
- sql - 插入后使用触发器更新表?