具有部分视图的 AJAX pagedlist

2023-11-23

我不太清楚如何使用 ajax 获取部分视图来呈现分页列表。

我最接近的工作是来自的例子在部分视图中使用分页,asp.net mvc

我基本上试图创建一个页面,其中包含每个用户的评论列表,其中该页面可以按照与 stackoverflow 用户页面上的答案选项卡相同的方式进行更改。

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

控制器:

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");
    }
}

Views:

索引.cshtml:

<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

<h2>List of Products</h2>

<div id="products">    
    @Html.Action("Products", "Product")
</div>


@section scripts{

    <script type="text/javascript">
    $(function() {
    $('#myPager').on('click', 'a', function() {
        $.ajax({
            url: this.href,
            type: 'GET',
            cache: false,
            success: function(result) {
                $('#products').html(result);
            }
        });
        return false;
    });
    });
    </script>
    }

_产品.cshtml:

@using PagedList.Mvc;
@using PagedList;


<ul>
    @foreach(var product in ViewBag.OnePageOfProducts){
        <li>@product.Name</li>
    }
</ul>

<!-- output a paging control that lets the user navigation to the previous page, next page, etc -->
<div id="myPager">
    @Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts, page => Url.Action("Products", new { page }))
</div>

Model

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
}

谁能告诉我我做错了什么?


我最终使用了 pagedlist 源代码中的不显眼的 ajax 示例 [https://github.com/troygoode/PagedList][1]

部分视图:

@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"}))

Index:

@{
    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();
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有部分视图的 AJAX pagedlist 的相关文章

随机推荐

  • 为什么无法在升级中更改放置限制?

    我的 Azure Service Fabric 群集中运行着无状态 ASP NET Core RC1 服务 它具有以下清单
  • 更新 knex 迁移中的枚举列类型

    我正在寻找编写一个迁移字符串以将新字符串添加到枚举列类型 我正在尝试添加gamma to the service column 我尝试使用下面的代码 这会发生冲突 因为表和列已经存在 const table user association
  • 具有相同案例名称和不同类型关联值的枚举

    以下 Swift 代码可编译 enum GraphDataSource case array data Double start Double step Double case pairs XYValues Double Double ca
  • Python脚本运行方式的差异

    我有一个非常简单的 Python 问题 其中包含使用 Django 的示例 运行 Python 脚本时 我是否总是必须在脚本文件名前面加上python命令 在我关注的Django教程中 一些命令如下 django admin py star
  • 如何监听“props”的变化

    In the VueJs 2 0 文档我找不到任何可以监听的钩子props变化 VueJs 有这样的钩子吗 onPropsUpdated 或类似的 Update 正如 wostex所建议的 我尝试watch我的财产 但没有任何改变 然后我意
  • 在 Rails 外部生成迁移

    我在 Rails 之外使用 ActiveRecord 我想要一个程序来生成迁移的框架 以及一个收集和维护它们的系统 有人可以提出建议吗 也来看看新品活动记录迁移
  • 两个日期之间的月份函数

    在oracle中 我可以找出使用MONTHS BETWEEN函数之间的月份数 在 postgres 中 我为此使用 extract 函数 例如 像 select extract year from age current date 2012
  • PHP和Mysql中实现哈希表的步骤

    我是编程语言新手 我正在使用 PHP 和 mysql 我收到一个用 php 做哈希表的作业 我需要做的是 存储用户收集的项目 然后显示它 在网上做了一些研究后 我将在实现哈希表时执行以下步骤 如果我错了 请纠正我 设置表格 gt 用户表 u
  • 使用反射覆盖私有方法

    NET 3 5 中是否可以使用 Reflection 重写私有方法 嗯 它需要是virtual可以覆盖它 通过编写从该类继承的动态类型 并且您不能拥有private virtual 这个不成立 你也许可以重写internal virtual
  • 从 .NET 程序集创建对象时,VB6 中出现后期绑定运行时错误

    我有一个 vb6 项目 其中引用了 vb net com 库 当我使用早期绑定时 该项目运行良好 例如 Dim b as object Set b new myComLib testObject 当我使用后期绑定时 例如 Dim b as
  • Mongoose 实例方法未定义

    我使用 Mongoose 定义了一个实例方法来验证代表 用户 RepSchema methods authenticate function password return this encryptPassword password thi
  • 类与 Class.new,模块与 Module.new

    有什么区别class and Class new module and Module new 我知道 Class new Module new创建一个匿名class module 当我们第一次将它分配给常量时 它就变成了常量的名称class
  • 基于 Razor 的视图看不到引用的程序集

    我正在尝试基于另一个程序集中的类创建强类型视图 但无论出于何种原因 我的 Razor 视图似乎看不到我的项目中引用的其他程序集 例如 model MyClasses MyModel 导致 Visual Studio 2010 中出现错误 类
  • Android Studio 中的 OpenCV

    我想在 Android Studio 的应用程序中使用 OpenCV 库 我按照说明发现here但我收到错误 未找到名称为 default 的配置 有什么问题吗 我使用 Android Studio 1 0 和 gradle 2 2 1 以
  • 在 wicked pdf 中 render_to_string 的正确方法是什么?

    这是 wicked pdf 文档中指定的内容 WickedPdf new pdf from string render to string pdf gt pdf file pdf template gt templates pdf html
  • Flutter Redux Navigator GlobalKey.currentState 返回 null

    我正在使用 Redux 开发 Flutter 当用户启动应用程序时 我想要Redux自动地dispatch an action 这一行动将使Navigator依赖地推送不同的路线 这个片段由 Flutter 开发成员提供使用GlobalKe
  • 在 Java 中将多个项目添加到已初始化的数组列表中

    My arraylist可能会根据用户设置以不同的方式填充 因此我已将其初始化为 ArrayList
  • React 路由器参数化路由:语法错误:预期表达式,得到 '<'

    我设置了一个使用参数化路由的 React router
  • Laravel:Auth 中间件中的多重防护

    我有一个 Laravel 应用程序 使用多用户表来扮演不同的角色 我添加了 2 个自定义守卫 如下所示 guards gt consumer gt driver gt session provider gt consumer member
  • 具有部分视图的 AJAX pagedlist

    我不太清楚如何使用 ajax 获取部分视图来呈现分页列表 我最接近的工作是来自的例子在部分视图中使用分页 asp net mvc 我基本上试图创建一个页面 其中包含每个用户的评论列表 其中该页面可以按照与 stackoverflow 用户页