Я пытаюсь двунаправленно сортировать данные, используя ASP.NET MVC и Entity Framework, данные сортируются для первой страницы (т. Е. Для первых 5 записей), но когда загружается вторая страница, сортировка не выполняется. сохранялось.
Метод действия индекса:
public ActionResult Index(string searchtext, int? page, string sortby, int? pagesize)
{
IEnumerable<tbl_Employee> emplist = objBal.GetAllEmp();
ViewBag.namesort = String.IsNullOrEmpty(sortby) ? "emp_name desc" : "";
ViewBag.gendersort = String.IsNullOrEmpty(sortby) ? "emp_gender desc" : "";
ViewBag.citysort = String.IsNullOrEmpty(sortby) ? "emp_city desc" : "";
//ViewBag.gendersort = (sortby == "emp_gender") ? "emp_gender" : "emp_gender desc";
//ViewBag.citysort = (sortby == "emp_city") ? "emp_city" : "emp_city desc";
int defaultpagesize = (pagesize ?? 5);
ViewBag.pgsize = defaultpagesize;
if (!String.IsNullOrEmpty(searchtext))
{
emplist = objBal.Searchemployee(searchtext);
}
switch (sortby)
{
case "emp_name desc":
emplist = emplist.OrderByDescending(name => name.emp_name);
break;
case "emp_city desc":
emplist = emplist.OrderByDescending(name => name.emp_city);
break;
case "emp_city":
emplist = emplist.OrderBy(name => name.emp_city);
break;
case "emp_gender desc":
emplist = emplist.OrderBy(name => name.emp_gender);
break;
case "emp_gender":
emplist = emplist.OrderBy(name => name.emp_gender);
break;
default:
emplist = emplist.OrderBy(name => name.emp_name);
break;
}
//var pageno = (page ?? 1);
//ViewBag.Pageno = pageno;
return View(emplist.ToPagedList(page ?? 1, defaultpagesize));
}
Index.cshtml
<tr>
<th>
@Html.ActionLink("Name", "Index", new { sortby = ViewBag.namesort })
</th>
<th>
@Html.ActionLink("City", "Index", new { sortby = ViewBag.citysort })
</th>
<th>
@Html.ActionLink("Gender", "Index", new { sortby = ViewBag.gendersort })
</th>
<th>
@Html.DisplayNameFor(model => model.First().emp_contact)
</th>
<th>
@Html.DisplayNameFor(model => model.First().emp_mailid)
</th>
<th>Action</th>
@Html.PagedListPager(Model, page => Url.Action("Index", new
{
page,
searchtext = Request["searchtext"],
soryby = Request["sortby"]@*, pagesize = ViewBag.pgsize*@ }),
new PagedListRenderOptions()
{
Display = PagedListDisplayMode.IfNeeded,
DisplayPageCountAndCurrentLocation = true
})