【发布时间】:2018-07-19 00:52:31
【问题描述】:
我是 MVC 的新手。我正在尝试基于页面上的一些过滤器创建一个网格结构,然后应用分页。一切正常,除了当我点击第二页时,另一个寻呼机控件被添加到页面上,导致页面上总共有两个寻呼机控件。任何帮助将不胜感激。以下是我的代码:
型号:
public class UnMatched
{
public List<string> List_BUnit { get; set; }
public PagedList<UnMatched> List_Grid { get; set; }
}
控制器:
public ActionResult Index(int? page)
{
return Request.IsAjaxRequest()
? (ActionResult)PartialView("AjaxMethod")
: View(PopulateBUnit());
}
public ActionResult AjaxMethod(string bunit, int? Page)
{
//Get lst_UnMatch here
return PartialView(List_Grid.ToPagedList(pageIndex, pageSize));
}
主视图:
@model MVC_Sample.Models.UnMatched
@using PagedList;
@using PagedList.Mvc;
@{
ViewBag.Title = "Home Page";
}
<head>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<div id="GridContainer" style="display:none;">
@Html.Partial("AjaxMethod", Model.List_Grid)
</div>
部分视图:
@model IPagedList<MVC_Sample.Models.UnMatched>
@using PagedList;
@using PagedList.Mvc;
<div id="GridDiv">
<table class="Grid">
<tr>
<th>
@Html.DisplayName("rec_FSRPT_ID")
</th>
<th>
@Html.DisplayName("BUNit")
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.rec_FSRPT_ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.BUNit)
</td>
</tr>
}
</table>
</div>
</div>
<div id="myPager">
@Html.PagedListPager(
Model,
page => Url.Action("AjaxMethod", new { page = page}),
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions(){
HttpMethod = "GET", UpdateTargetId = "GridDiv" }))
</div>
<script>
$(function () {
$('#myPager').on('click', 'a', function () {
$.ajax({
url: this.href,
type: 'GET',
cache: false,
success: function (result) {
$('#myPager').html(result);
}
});
return false;
});
});
</script>
寻呼机控制截图:
【问题讨论】:
-
我不知道是不是错字,但是你的html元素没有正确关闭。
-
感谢您的检查。这些是拼写错误,现在已更正,所以如果您可以查看更新的错误并提出建议。
-
this.href的值是多少?看起来您正在重新加载#myPager内的整个页面。
标签: asp.net-mvc