0

I am using attribute routing in MVC 5 application on a controller which is used to fetch list and performs searching sorting and pagination :

    [HttpGet]    
    [Route("Admin/UserList/{searchBy?}/{search?}/{page?}/{sortBy?}")]  
    public ActionResult AdminUserList(string searchBy, string search, int? 
           page, string sortBy)
    {  
       IEnumerable<Employee> employees=new List<Employee>();
        string emailid=Session["EmailId"].ToString();
        var role = (from users in db.Employees where users.EmailId == 
         emailid select users.Role).SingleOrDefault();
        if (role==Roles.superAdmin)
        {
            employees = (from users in db.Employees where users.Role == 
            Roles.user || users.Role == Roles.admin select 
            users).AsEnumerable();

        }

        else
        {
            employees = (from users in db.Employees where users.Role == Roles.user select users).AsEnumerable();           
        }         
            ViewBag.NameSort = String.IsNullOrEmpty(sortBy) ? "Name desc" : ""; 


            if (searchBy == "Email ID")
            {
                employees = employees.Where(x => search == null || x.EmailId.Contains(search));
            }
            else
            {
                employees = employees.Where(x => search == null || x.Name.Contains(search));
            }

            switch (sortBy)
            {
                case "Name desc":
                    employees = employees.OrderByDescending(x => x.Name);
                    break;
                default:
                    employees = employees.OrderBy(x => x.Name);
                    break;
            }
            return View(employees.ToPagedList(page ?? 1, 6));              
    }

For a URL - rootURL/Admin/UserList/name - it is working fine and fetching all the list of users by the name since {search} = null .

But if a user wants to sort by "Name desc",then url will be - rootURL/Admin/UserList/name/Name%20desc,

in this few params like {page},{search} is not specified in the URL and how will controller recognize the same.

I have tried to provide two different routes to the controller but it will take many combinations. Like -

[Route("Admin/UserList/{searchBy?}/{search?}/{sortBy?}")]
[Route("Admin/UserList/{searchBy?}/{search?}/{page?}/{sortBy?}")]
and so on...

How we will map this request in an optimal way -

RootUrl/Admin/UserList/name/t/Name%20desc

i.e. to search if a name has letter "t" and show result by sorting name in descending order.

Also how will I map the request to the same controller using razor?

@Html.PagedListPager(Model, page => Url.Action("UserList", new
   {
       page,
       searchBy = Request.QueryString["searchBy"],
       search = Request.QueryString["search"],
       sortBy = Request.QueryString["sortBy"]
    }),
      new PagedListRenderOptions() { Display = 
       PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = 
      true })

Thank you for your help.

  • Only the last parameter can be marked as optional –  Apr 27 '18 at 07:06
  • What if there are two optional parameters? @StephenMuecke – Deepankshee Jain Apr 27 '18 at 07:13
  • If you have multiple optional parameters, then the routing engine will always generate query string values, not route values (unless all segments are provded). How could it possibly what a url which is `Admin/UserList/X/Y` means - is `X` the value for `searchBy` or `search` or `page` or `sortBy` - ditto for `Y` - only the last parameter can be optional if you want route values) –  Apr 27 '18 at 07:18
  • okay thank you for clarifying it. @StephenMuecke – Deepankshee Jain Apr 27 '18 at 07:33

0 Answers0