I have this piece of code in controller where it accepts a class as a parameter
public ActionResult Index(PaginationModel paginationViewModel = null)
{
//do some logic here
return View(viewModel);
}
And here's what the model looks like
public class PaginationModel
{
protected const int DisplayPageRange = 2;
//Pagination Related Properties
public int CurrentPage { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
public string PageName { get; set; }
public int ItemsPerPage { get; set; }
public int DefaultItemsPerPage { get; set; }
public string PagingText { get; set; }
//Filters
public string Search { get; set; }
public string Category { get; set; }
public string Star { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
}
I'm calling the action in the view like
<a href="/?Category=@genre">@genre</a>
Everything seems to work fine but I want to format the URL so it will looks like
Movie/Index/Horror/1 //Horror is the category and 1 is the current page number
Currently, it's showing
Movie/Index?Category=Horror&Page=1
I can do it using separate parameters instead of using a class in the action like
public ActionResult Index(string category, int pagenum, etc etc)
{
}
But is there a way to do this in RouteConfig with class as a parameter?
Edit:
My RouteConfig is
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MovieSearch",
url: "Movie/Index/{Category}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "AdminSearch",
url: "{controller}/{action}/{key}",
defaults: new {controller = "Admin", action = "Search", id = UrlParameter.Optional}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Movie", action = "Index", id = UrlParameter.Optional }
);
}