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

Location of Validation Message
ASP.NET Identity update column type for primary key Id in AspNetUsers table from a string to and int or a guid, yes or no?
Deploying ASP MVC 5 App with IIS 7.5
validation for dropdown in MVC3
MVC getting a list of music and image into a table
How to allow users to only edit their details in a catalog
Defining Repository for MVC best practice
MVC ASP.NET search filter by int
How do I add placeholder text from the model into a MVC view?
Web site analytic application in the mvc application
mvc3 EditorFor/DisplayFor throws Collection was modified; enumeration operation may not execute
When I update ASP.NET MVC from 5.0 to 5.1, do I need to update the bindingRedirect to 5.1?
Set ClaimsAuthorizationManager in Code (MVC 5)
KendoUi MVC Grid passing parameter values
How exactly does CSS/Javascript minification and bundling work in MVC?
Roles Comparison in MVC

Categories

HOME
apache-spark
lsf
isis
path
word-vba
jsonschema
xquery
active-directory
spacemacs
whitespace
py.test
struts2-jquery
cross-compiling
pymc3
doorkeeper
highlight
calabash-android
accumulo
vast
lookup-tables
stackexchange-api
formio
revitpythonshell
atg
django-autocomplete-light
datamatrix
sax
point-cloud-library
f2py
kubernetes-go-client
directx-12
kube-dns
pvlib
apex-code
tensor
extraction
jdom-2
powerbuilder-conversion
vue2
solid-principles
amazon-elasticsearch
tibco-ems
csr
hydra
hostapd
pseudo-element
clientscript
spring-form
okular
glfw
ammonite
netbeans-7
swift-package-manager
jeditorpane
itamae
vb4android
webclient-download
xmllint
dms
mongolab
aplpy
training-data
jedi
openejb
user-registration
selectsinglenode
lossless-compression
java-client
sendto
django-postgresql
mui
publish-actions
mouseenter
nine-patch
sirtrevor
xml.modify
glulookat
programmers-notepad
wpf-4.0
makumba
sticky-footer
httpcontext.cache
settings.bundle
process.start
ryz
xml-database
server.transfer
ants

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