Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MVC#中通过属性路由使用分页时,如何跳过可选的空参数?_C#_Asp.net Mvc_Pagination_Attributerouting - Fatal编程技术网

在MVC#中通过属性路由使用分页时,如何跳过可选的空参数?

在MVC#中通过属性路由使用分页时,如何跳过可选的空参数?,c#,asp.net-mvc,pagination,attributerouting,C#,Asp.net Mvc,Pagination,Attributerouting,我在控制器上的MVC 5应用程序中使用属性路由,该控制器用于获取列表并执行搜索、排序和分页: [HttpGet] [Route("Admin/UserList/{searchBy?}/{search?}/{page?}/{sortBy?}")] public ActionResult AdminUserList(string searchBy, string search, int? page, string sortBy) {

我在控制器上的MVC 5应用程序中使用属性路由,该控制器用于获取列表并执行搜索、排序和分页:

    [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));              
    }

谢谢您的帮助。

只有最后一个参数可以标记为可选。如果有两个可选参数怎么办@StephenMueckei如果您有多个可选参数,那么路由引擎将始终生成查询字符串值,而不是路由值(除非提供了所有段)。它怎么可能是
Admin/UserList/X/Y
的url的意思呢?X是
searchBy
searchBy
page
sortBy
的值?如果您想要路由值,只有最后一个参数是可选的)好的,谢谢您的澄清@斯蒂芬穆克
@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 })