0

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 }
    );
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
  • You need to create a specific route - `url: "Movie/Index/{Category}/{CurrentPage }` and locate it before the default route. –  Mar 13 '17 at 06:11
  • I did that, its before the Default route but still going to the default route I guess? – Cornelio Cawicaan Jr Mar 13 '17 at 06:16
  • You have not shown what you have done! (how can we guess your mistake) –  Mar 13 '17 at 06:17
  • It does not have a `{CurrentPage}` paremeter (although not sure if you mean `CurrentPage` or `Page`). And you need to use `@Html.ActionLink()` to generate it (or `Url.Action()` for the `href`attribute if you want to generate the `` tag manually –  Mar 13 '17 at 06:25
  • Ha! Changing to use ActionLink actually solves the issue. Thanks heaps! Upvoting – Cornelio Cawicaan Jr Mar 13 '17 at 06:38

0 Answers0