I am Doing Paging in MVC on Index Page ..on this Line I got error
return View( employee.ToPagedList(Page ?? 1,3));
here is Index Method
public ActionResult Index(string searchBy, string search, int? Page, string sortBy)
{
ViewBag.SortNameParameter = string.IsNullOrEmpty(sortBy) ? "Name desc"
: "";
ViewBag.SortGenderParameter = string.IsNullOrEmpty(sortBy) ? "Gender desc"
: "Gender";
var employee = db.Employees.AsQueryable();
if (searchBy == "Gender")
{
employee = employee.Where(x => x.Gender == search || search == null);
}
else
{
employee = employee.Where(x => x.FUllName .StartsWith(search ) || search == null);
}
switch (sortBy)
{
case "Name desc":
employee = employee.OrderByDescending(x => x.FUllName);
break;
case "Default":
employee = employee.OrderBy(x => x.FUllName);
break;
}
return View( employee.ToPagedList(Page ?? 1,3));
}
I am not using Skip method ... But there is this error :
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
I read Similar Posts
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'
How to solve "The method 'Skip' is only supported for sorted input in LINQ to Entities."
The method ‘Skip’ is only supported for sorted input in LINQ to Entities. The method ‘OrderBy’ must be called before the method ‘Skip’
ASP.NET MVC 3 PagedList. The method 'Skip' is only supported for sorted input in LINQ to Entities.
and did following changes ...
1.Put employee.Where code before the switch statement:
2.Add a default case in your switch statement, and make it throw. case "Default": throw new ArgumentException("error", sortBy);
Use the type IOrderedQueryable.
IQueryable employee = db.Employees.AsQueryable();
this doesnot solve problem ..
mostly they use to have Skip method .. But i Dont have ... and Other posts query are complex one ..
Please suggest whats missing
You do have a Skip
method.
The PagedList
added it for you. Check the code out. That's how paging works, Take
and Skip
.
Also I think your case statement was meant to be
switch (sortBy)
{
case "Name desc":
employee = employee.OrderByDescending(x => x.FUllName);
break;
default: // Not: case "Default"
employee = employee.OrderBy(x => x.FUllName);
break;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With