【问题标题】:C# PagedList MVC add #anchor in paginationC# PagedList MVC 在分页中添加#anchor
【发布时间】:2015-05-06 23:25:03
【问题描述】:

我想知道是否可以在 ?Page=1 之后添加 PagedList 和 #anchor

示例:http://localhost:10220/Products/MyUrl/Sku?Page=1#Products

我有:

 @Html.PagedListPager((IPagedList)ViewBag.Products, page => Url.Action("Index", new { page }))

为什么我在我的产品之前有一些内容,我不希望我的客户在进行分页时一直滚动页面

obs:或者我可以让我的 PagedList 不使用 ajax 刷新页面吗?有人举个例子吗?

【问题讨论】:

  • 您的 localhost URL 在 interwebz 上不起作用。

标签: c# asp.net-mvc pagedlist


【解决方案1】:

怎么样:

Url.Action("Index", new { page }) + "#Products"

无法使用路由助手添加片段(您称之为“锚”),因为片段不是路由的一部分;他们只适用于客户端。无论如何,lambda 可以接受任何有效的表达式,而不仅仅是单方法调用,Url.Action 只返回一个字符串。因此,您只需将片段附加到字符串的末尾即可结束。

【讨论】:

    【解决方案2】:

    在我看来,最好的方式总是使用 ajax!性能提升

    您的控制器代码:

       public class MyCutomModel
    {
    
      public int Id { get; set; }
      public string Name { get; set; }
    }
    public class HomeController : Controller
    
      //
      // GET: /Home/
      public ActionResult Index(int page = 1)
      {
         List<MyCutomModel> model = new List<MyCutomModel>();
    
         for (int i = 0; i < 10; i++)
         {
            model.Add(new MyCutomModel { Id = i, Name = "Name " + i.ToString() });
         }
    
         if (Request.IsAjaxRequest())
         {
            return PartialView("_Index", model.ToPagedList(page, 4));
         }
    
         return View(model.ToPagedList(page, 4));
      }
    }
    

    您的索引视图:

    @model PagedList<MVCApp.Controllers.MyCutomModel>
    @{
       ViewBag.Title = "Index";
    }
    @DateTime.Now
    
    @Html.Partial("_Index", Model)
    

    您的索引部分视图(“_Index.cshtml”):

    @model PagedList<MVCApp.Controllers.MyCutomModel>
    <div id="replaceDiv">
       <table class="table">
          <tbody>
             @foreach (var item in Model)
             {
               <tr>
                   <td>@Html.DisplayFor(modelItem => item.Name)</td>
                </tr>
             }
          </tbody>
       </table>
       @Html.PagedListPager(Model, page => Url.Action("Index", new { page,   sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "replaceDiv" }))
    </div>
    

    注意 PagedListPager 的结尾,这是秘密

    PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "replaceDiv" }))
    

    【讨论】:

    • 不要忘记确保 是参考
    • 确保您在您的页面上引用 并在您的 webconfig 上引用最后一个案例,确保它是真的键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多