C# PagedList MVC 在分页中添加#anchor
C# PagedList MVC add #anchor in pagination
我想知道我是否可以让我的 PagedList 在 ?Page=1
之后添加和 #anchor
示例:http://localhost:10220/Products/MyUrl/Sku?Page=1#Products
我有:
@Html.PagedListPager((IPagedList)ViewBag.Products, page => Url.Action("Index", new { page }))
为什么我的产品之前有一些内容,我不希望我的客户在进行分页时一直滚动页面
obs:或者我可以让我的 PagedList 不使用 ajax 刷新页面吗?有人有一个例子吗?
怎么样:
Url.Action("Index", new { page }) + "#Products"
无法使用路由助手添加片段(您称之为 "anchor"),因为片段不是路由的一部分;他们只适用于客户端。无论如何,lambda 可以接受任何有效的表达式,而不仅仅是单一方法调用,而 Url.Action
只是 returns 一个字符串。因此,您只需将片段添加到字符串的末尾,然后就可以结束了。
在我看来,最好的方法总是使用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" }))
我想知道我是否可以让我的 PagedList 在 ?Page=1
之后添加和 #anchor示例:http://localhost:10220/Products/MyUrl/Sku?Page=1#Products
我有:
@Html.PagedListPager((IPagedList)ViewBag.Products, page => Url.Action("Index", new { page }))
为什么我的产品之前有一些内容,我不希望我的客户在进行分页时一直滚动页面
obs:或者我可以让我的 PagedList 不使用 ajax 刷新页面吗?有人有一个例子吗?
怎么样:
Url.Action("Index", new { page }) + "#Products"
无法使用路由助手添加片段(您称之为 "anchor"),因为片段不是路由的一部分;他们只适用于客户端。无论如何,lambda 可以接受任何有效的表达式,而不仅仅是单一方法调用,而 Url.Action
只是 returns 一个字符串。因此,您只需将片段添加到字符串的末尾,然后就可以结束了。
在我看来,最好的方法总是使用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" }))