asp.net-mvc


How do I implement paging on a route with other parameters?


I know how to implement paging on a route when there's a single route such as /Dinners/Page/1. My issue is that I have a list of articles. These articles are sat in categories and subcategories.
If no category/subcategory is specified I want to return all articles.
If only category is supplied I supply all articles in that category
If category and subcategory are supplied I want only the articles in the subcategory.
I had this working no problem. Then I wanted to add paging. I wanted a route that could do the following:
domain.com/6 - goes to page 6 of all articles
domain.com/category/2 - goes to page 2 of category articles
domain.com/category/subcategory/3 - goes to page 3 of subcategory articles
I can't get this working because the first example is looking for category 6, the second is looking for category 2. I've tried adding a constraint in the hope the numeric value would be attributed to page. No joy.
Also, I want to do this server side.
Any ideas? Here's the route in my RouteConfig:
routes.MapRoute(
name: "Category",
url: "{category}/{subcategory}/{page}",
defaults: new { controller = "Articles", action = "Index", category= UrlParameter.Optional, subcategory = UrlParameter.Optional, page = UrlParameter.Optional }
//, constraints: new { page = #"\d+" }
);
Ok. I played around with the route and added three routes to replace the original.
The first one captures the view all with the paging. If no paging is provided I just display the first page. The second route is the main category with paging.
The third route covers off if there is a subcategory. This is working like a charm now. :)
Here are my routes in case you want to know exactly what I did:
routes.MapRoute(
name: "Paged",
url: "{page}",
defaults: new { controller = "Articles", action = "Index", page = UrlParameter.Optional },
constraints: new { page = #"\d+" }
);
routes.MapRoute(
name: "PagedCategory",
url: "{category}/{page}",
defaults: new { controller = "Articles", action = "Index", category = UrlParameter.Optional, page = UrlParameter.Optional }
//, constraints: new { page = #"\d+" }
);
routes.MapRoute(
name: "PagedSubCategory",
url: "{category}/{subcategory}/{page}",
defaults: new { controller = "Articles", action = "Index", category = UrlParameter.Optional, subcategory = UrlParameter.Optional, page = UrlParameter.Optional }
//, constraints: new { page = #"\d+" }
);
Pass page number as querystring parameter, like: category/subcategory?page=1.
Or change you route like this,
routes.MapRoute(
name: "Category",
url: "{controller}/{action}/{category}/{subcategory}/{page}",
defaults: new { controller = "Articles", action = "Index", category= UrlParameter.Optional, subcategory = UrlParameter.Optional, page = UrlParameter.Optional }
//, constraints: new { page = #"\d+" }
);
Hope this works, thank you.

Related Links

ASP.Net MVC - Cookie Remember Me
Why is MVC page compilation forcing constant app restarts on my Azure web app?
Bootstrap Modal in separate view [duplicate]
asp.net core authentication with IdentityDbContext<AppUser, AppRole, int, AppUserClaim, AppUserRole, AppUserLogin, AppRoleClaim, AppUserToken>
Access Linq Output in MVC Controller itself
Call a controller method with parameters from partial view mvc 5
How do I publish my MVC site on IIS 7+ with proper URLs?
.ASPNET.Cookies cookie is not creating during Azure AD authentication [duplicate]
Save List of object in MVC 5
Using Automapper to update an existing Entity with less Properties
Entity Framework 6 DropCreateDatabaseIfModelChanges
Optgroup in DropDownlistFor MVC 4 Not Support Mvc validation
MS Edge Removes Space From String Return From MVC
MVC Entity Framework Error on Mapping Entity with [NotMapped] properties
ASP.NET MVC 5 - Databind with bootstrap form helpers
Entity Framework code first - foreign key relationship

Categories

HOME
graph
sharepoint-2013
linear-programming
fpdf
encog
r-caret
tin-can-api
clion
wso2-das
pie-chart
openframeworks
cytoscape.js
axure
editor
jquery-jtable
imp
raspberry-pi2
teraterm
procedure
appcelerator-alloy
accelerated-mobile-page
editorconfig
asp.net-identity-2
nest-thermostat
edit-distance
draggable
ubuntu-server
bits
msal
sinon
jpype
linear
project-centennial
syncfusion
mergesort
couchbase-lite
distributed-lock
virtualmin
hostapd
manifoldjs
federation
notimplementedexception
database-cleaner
harvest-scm
haste
omniauth
tastypie
getelementbyid
mapr
android-sdk-tools
shift-reduce-conflict
restbed
enhanced-ecommerce
jstorm
main-activity
java-gstreamer
jedi
session-replication
rdotnet
kue
misra
ash
django-supervisor
bunny
image-scaling
dojo.gridx
nhunspell
twill
fig
magic-unipaas
cfile
libavcodec
mms-gateway
impdp
memento
layout-manager
cellid
httpconnection
junitperf
web-based
reference-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App