gpt4 book ai didi

c# - 在razor View中设置分页页面大小DropDownList

转载 作者:行者123 更新时间:2023-11-30 17:38:36 25 4
gpt4 key购买 nike

我想在我的 asp.net web 表单中使用下拉列表选择页面大小。实际上我已经做到了,但是当我从下拉列表中更改页面大小时,没有任何改变。我想我需要在我的 Controller 中写一些额外的东西,或者我的 View 可能是错误的?

我的 Index.cshtml View :

@model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using (Html.BeginForm("Index", "Users"))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" class="btn btn-primary" value="Search" />
</p>
}
<table class="table">
<tr>
<th>@Html.ActionLink("Login","Index", new { sortOrder = ViewBag.LoginSortParm, currentFilter = ViewBag.CurrentFilter })</th>
</tr>
@foreach (var item in Model.Model2) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Login)</td>
</tr>
}
</table>

Page @(Model.Model2.PageCount < Model.Model2.PageNumber ? 0 : Model.Model2.PageNumber) of @Model.Model2.PageCount
@Html.PagedListPager(Model.Model2, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }) )

Select page size @Html.DropDownList("PageSize", new SelectList(new Dictionary<string, int> { { "10", 10 }, { "20", 20 }, { "100", 100 } }, "Key", "Value", Model.Model2.Count))

这是我的 Controller 。在这里我没有添加任何关于 DropDownList 的内容:

public ActionResult Index(string sortOrder, string currentFilter, string searchString,  int? page)
{
ViewBag.CurrentSort = sortOrder;
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var users = from s in db.Users.Include(u => u.CustomerProject).Include(u => u.Service).Include(u => u.Customer) select s;
int pageSize = 20;
int pageNumber = (page ?? 1);
var inst = new TimeReportViewMod();
inst.Model1 = users.ToList();
inst.Model2 = users.ToPagedList(pageNumber, pageSize);
return View("Index", inst);
}

最佳答案

你需要在你的下拉列表中包含 <form>标签所以它的值被提交给 Controller 方法,并且该方法还需要一个额外的参数来绑定(bind)到该值。您还需要在 Html.PagedListPager() 中包含所选值以便在您转到另一个页面时保留它。

您还没有显示您的 TimeReportViewMod模型,但奇怪的是它包含属性 IEnumerable<User>IPagedList<User> , 这也是 IEnumerable<User>所以第一个是不必要的。但它应该包含的是您用于搜索和过滤的属性,以便您可以强绑定(bind)到您的属性并避免使用 ViewBag .它应该看起来像

public class TimeReportVM
{
[Display(Name = "Find by name")]
public string SearchString { get; set; }
public string SortOrder { get; set; }
[Display(Name = "Select page size")]
public int PageSize { get; set; }
public SelectList PageSizeList { get; set; }
public IPagedList<User> Users { get; set; }
}

注:不清楚是什么sortOrder是为了 - 你永远不会在任何地方使用它。您也从不使用 searchString 的值/currentFilter在你的查询中。

那么你的 Controller 方法是

public ActionResult Index(string sortOrder, string searchString, int pageSize, int? page)
{
if (searchString != null)
{
page = 1;
}
// this query should be taking into account the values of sortOrder and searchString?
var users = from s in db.Users.Include(u => u.CustomerProject).Include(u => u.Service).Include(u => u.Customer) select s;
TimeReportVM model = new TimeReportVM()
{
SortOrder = sortOrder,
SearchString = searchString,
PageSize = pageSize,
PageSizeList = new SelectList(new int[]{ 10, 20, 100 }),
Users = users.ToPagedList(page ?? 1, pageSize);
};
return View(model);
}

在 View 中

@model TimeReportingWebApp.TimeReportViewMod
@using PagedList.Mvc;
....
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.SearchString)
@Html.TextBoxFor(m => m.SearchString)
@Html.LabelFor(m => m.PageSize)
@Html.DropDownListFor(m => m.PageSize, Model.PageSizeList)
<input type="submit" class="btn btn-primary" value="Search" />
}
....
Page @(Model.Users.PageCount < Model.Users.PageNumber ? 0 : Model.Users.PageNumber) of @Model.Users.PageCount
@Html.PagedListPager(Model.Users, page => Url.Action("Index",
new { page, sortOrder = Model.SortOrder, searchString = Model.SearchString, pageSize = Model.PageSize }) )

关于c# - 在razor View中设置分页页面大小DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com