我在mvc中搜索排序和分页演示,演示工作正常,我指定默认页面大小为5,我应该使用dropdownlist动态更改页面大小?部分代码: -
控制器操作: -
public ActionResult Index(string searchBy,string search,int? Page,string sortBy)
{
ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Name desc" : "";
ViewBag.SortGenderParameter = sortBy == "Gender" ? "Gender desc" : "Gender";
var employees = db.Employees.AsQueryable();
if(searchBy == "Gender")
{
employees = employees.Where(x => x.EmpGender == search || search == null);
}
else
{
employees = employees.Where(x => x.EmpName.StartsWith(search) || search == null);
}
switch(sortBy)
{
case "Name desc":
employees = employees.OrderByDescending(x=>x.EmpName);
break;
case "Gender desc":
employees = employees.OrderByDescending(x=>x.EmpGender);
break;
//case "Gender ":
//employees = employees.OrderByDescending(x=>x.EmpGender);
//break;
default:
employees = employees.OrderByDescending(x=>x.EmpName);
break;
}
return View(employees.ToPagedList(Page ?? 1, 5));
}
我在视图结尾添加了pagedlistpager,如下所示: -
@Html.PagedListPager(Model, Page => Url.Action("Index", new { Page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"], sortBy = Request["sortBy"] }),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true, DisplayItemSliceAndTotal = true })
答案 0 :(得分:0)
嗨很简单,只需使用jquery在dd change事件中将页面大小作为dd值发送。最后在控制器动作方法中,将该参数传递给ToPagedList()方法。
例如:
public ActionResult Index(string searchBy,string search,int? Page,string sortBy,int ddpagesize)
{
ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Name desc" : "";
ViewBag.SortGenderParameter = sortBy == "Gender" ? "Gender desc" : "Gender";
var employees = db.Employees.AsQueryable();
if(searchBy == "Gender")
{
employees = employees.Where(x => x.EmpGender == search || search == null);
}
else
{
employees = employees.Where(x => x.EmpName.StartsWith(search) || search == null);
}
switch(sortBy)
{
case "Name desc":
employees = employees.OrderByDescending(x=>x.EmpName);
break;
case "Gender desc":
employees = employees.OrderByDescending(x=>x.EmpGender);
break;
//case "Gender ":
//employees = employees.OrderByDescending(x=>x.EmpGender);
//break;
default:
employees = employees.OrderByDescending(x=>x.EmpName);
break;
}
return View(employees.ToPagedList(Page ?? 1, ddpagesize));
}
对于dropdownchange事件,本文将有所帮助:http://www.c-sharpcorner.com/blogs/drop-down-list-selected-index-changed-event-in-mvc
由于
KARTHIK