I am stuck in a weird situation. I have a controller action which is passed a category string. Then the action method matches the string in the database and collects the sub categories and products for it. Now, What I want is to achieve pagination for the products if they are greater than 10. The view is a strongly-typed one for Category Model
Following is the code for action method.
public ActionResult Catalog(string id)
{
Category catalog = pe.Categories.Where(cat => cat.CategoryName == id).Single();
return View(catalog);
}
I have done pagination in my other project like following but in that case the collection was an IQueryable which can’t be in the above case since its just for one category. Also, I am already passing an argument in the above function so how can i pass two.
public ViewResult Index(int? page)
{
IQueryable<Album> albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist).OrderBy(a => a.Title);
int pageIndex = page ?? 1;
return View(albums.ToPagedList(pageIndex, PageSize));
}
Please tell me how will you tackle such scenario.
Use a viewmodel to collect just the information that you want to show in your view. In your selection logic, select into the viewmodel the top-level attributes from the category and the relevant parts of the association. You can certainly have more than one parameter to the method. Supply the extra parameters to the route values in your ActionLink or Form helper methods and it should construct the Url properly to reference your action. Note you will need to change the type of the model in the view as well to match that from the action.
View (sample)