带有部分视图的AJAX页面列表

带有部分视图的AJAX页面列表,ajax,asp.net-mvc,pagedlist,Ajax,Asp.net Mvc,Pagedlist,我不太明白如何使用ajax获得局部视图来呈现分页列表 我能找到的最接近工作的例子是 我基本上是在尝试创建一个页面,其中每个用户都有一个评论列表,页面的更改方式与stackoverflow用户页面上的answers选项卡相同 在第一次单击寻呼机时,分页工作正常,但当我再次单击寻呼机时,返回的就是部分视图 控制器: public class ProductController : Controller { public IQueryable<Product> products =

我不太明白如何使用ajax获得局部视图来呈现分页列表

我能找到的最接近工作的例子是

我基本上是在尝试创建一个页面,其中每个用户都有一个评论列表,页面的更改方式与stackoverflow用户页面上的answers选项卡相同

在第一次单击寻呼机时,分页工作正常,但当我再次单击寻呼机时,返回的就是部分视图

控制器:

public class ProductController : Controller
{
    public IQueryable<Product> products = new List<Product> { 
    new Product{ProductId = 1, Name = "p1"},
     new Product{ProductId = 2, Name = "p2"},
      new Product{ProductId = 3, Name = "p3"},
       new Product{ProductId = 4, Name = "p4"},
        new Product{ProductId = 5, Name = "p5"}
    }.AsQueryable();

    public object Index()
    {         
        return View();
    }

    public object Products(int? page)
    {
        var pageNumber = page ?? 1; // if no page was specified in the querystring, default to the first page (1)
        var onePageOfProducts = products.ToPagedList(pageNumber, 3); // will only contain 25 products max because of the pageSize

        ViewBag.OnePageOfProducts = onePageOfProducts;
        return PartialView("_Products");
    }
}
  public class UnobtrusiveAjaxController : BaseController
    {
        // Unobtrusive Ajax
        public ActionResult Index(int? page)
        {
            var listPaged = GetPagedNames(page); // GetPagedNames is found in BaseController
            if (listPaged == null)
                return HttpNotFound();

            ViewBag.Names = listPaged;
            return Request.IsAjaxRequest()
                ? (ActionResult)PartialView("UnobtrusiveAjax_Partial")
                : View();
        }
    }

有人能告诉我我做错了什么吗?

我最终使用了来自pagedlist源代码的不引人注目的ajax示例[

局部视图:

@using PagedList;
@using PagedList.Mvc;    

<ul id="names" start="@ViewBag.Names.FirstItemOnPage">
    @foreach(var i in ViewBag.Names){
        <li>@i</li>
    }
</ul>

@Html.PagedListPager((IPagedList)ViewBag.Names, page => Url.Action("Index", new { page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions(){  HttpMethod = "GET", UpdateTargetId = "unobtrusive"}))

以防万一,因为原始问题没有得到回答。我想问题是点击处理程序没有重新连接到AJAX请求生成的新寻呼机元素。在这种情况下,我也不喜欢非结构化的AJAX解决方案,因为寻呼机id在嵌套视图中硬编码,而以其他方式传递它可能太麻烦

<script type="text/javascript">
   // better not to clutter global scope of course, just for brevity sake
   var attachHandlers = function() {
       $('#myPager a').click(function() {
           $('#myPager').load(this.href, function() {
               attachHandlers();
           });
           return false;
       });
   };

   $(document).ready(function () {
       attachHandlers();
   });
</script>

//当然,为了简洁起见,最好不要把全局范围弄乱
var attachHandlers=函数(){
$('#myPager a')。单击(函数(){
$('#myPager').load(this.href,function(){
attachHandlers();
});
返回false;
});
};
$(文档).ready(函数(){
attachHandlers();
});

ActionResult始终需要索引,否则您将被重定向到控制器/middleaction/?page=2&pageSize=sizeI需要在索引中包含以下脚本才能工作:@section Scripts{}
@using PagedList;
@using PagedList.Mvc;    

<ul id="names" start="@ViewBag.Names.FirstItemOnPage">
    @foreach(var i in ViewBag.Names){
        <li>@i</li>
    }
</ul>

@Html.PagedListPager((IPagedList)ViewBag.Names, page => Url.Action("Index", new { page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing( new AjaxOptions(){  HttpMethod = "GET", UpdateTargetId = "unobtrusive"}))
@{
    ViewBag.Title = "Unobtrusive Ajax";
}
@using PagedList;
@using PagedList.Mvc;

@Styles.Render("~/Content/PagedList.css")

<h2>Unobtrusive Ajax</h2>

<p>Example of paging a list:</p>
<div id="unobtrusive">
    @Html.Partial("UnobtrusiveAjax_Partial")
</div>
  public class UnobtrusiveAjaxController : BaseController
    {
        // Unobtrusive Ajax
        public ActionResult Index(int? page)
        {
            var listPaged = GetPagedNames(page); // GetPagedNames is found in BaseController
            if (listPaged == null)
                return HttpNotFound();

            ViewBag.Names = listPaged;
            return Request.IsAjaxRequest()
                ? (ActionResult)PartialView("UnobtrusiveAjax_Partial")
                : View();
        }
    }
<script type="text/javascript">
   // better not to clutter global scope of course, just for brevity sake
   var attachHandlers = function() {
       $('#myPager a').click(function() {
           $('#myPager').load(this.href, function() {
               attachHandlers();
           });
           return false;
       });
   };

   $(document).ready(function () {
       attachHandlers();
   });
</script>