C#页面列表MVC添加#分页中的定位
我想知道我是否可以将我的页面列表添加并在页面=1后定位 例如: 我有: @Html.PagedListPager((IPagedList)ViewBag.Products,page=>Url.Action(“Index”,new{page})) 为什么我在我的产品之前有一些内容,我不希望我的客户在分页时一直滚动页面 obs:或者我可以让我的页面列表不使用ajax刷新页面吗?有人举了一个例子?那么:C#页面列表MVC添加#分页中的定位,c#,asp.net-mvc,pagedlist,C#,Asp.net Mvc,Pagedlist,我想知道我是否可以将我的页面列表添加并在页面=1后定位 例如: 我有: @Html.PagedListPager((IPagedList)ViewBag.Products,page=>Url.Action(“Index”,new{page})) 为什么我在我的产品之前有一些内容,我不希望我的客户在分页时一直滚动页面 obs:或者我可以让我的页面列表不使用ajax刷新页面吗?有人举了一个例子?那么: Url.Action("Index", new { page }) + "#Products"
Url.Action("Index", new { page }) + "#Products"
无法使用路由助手添加片段(您称之为“锚”),因为片段不是路由的一部分;它们只应用于客户端。无论如何,lambda可以接受任何有效的表达式,而不仅仅是一个方法调用,而且
Url.Action
只返回一个字符串。因此,您可以将您的片段固定到字符串的末尾,并结束它。在我看来,最好的方法是始终使用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));
}
}
您的localhost URL在interwebz上不起作用。别忘了确保是引用确保您在页面上引用的是您的内容,在您的webconfig上引用的是最后一个案例,确保密钥为true
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)
@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>
PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "replaceDiv" }))