如何在ASP.NET MVC 5中进行分页路由

时间:2016-06-10 09:13:53

标签: asp.net-mvc asp.net-mvc-5 pagination routing

我正在为ASP.NET MVC 5中的应用程序进行分页,并坚持使用分页的路由。以下是我尝试使用的格式:

<a href="~/Home/page/1)">Next</a>

目前,它适用于以下方面:

<a href="~/Home?page=1">Next</a>

在RouteConfig文件中,我设置了以下路线:

public static void RegisterRoutes(RouteCollection routes)
    {
        //routes.RouteExistingFiles = true;
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute( //Route For The Pagination
            "Home",
            "Home/page/{page}",
             new { controller = "Home", action = "Index" }
       );

        routes.MapRoute( //Default Route
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

控制器:

    public ActionResult Index(int? page)
    {
        MainDbContext db = new MainDbContext();

        var listItems = new List<List>();

        if (User.Identity.IsAuthenticated)
        {
            listItems = (db.Lists.ToList());
        }
        else
        {
            listItems = (db.Lists.Where(m => m.Public == "Yes").ToList());
        }

        var pager = new Pager(listItems.Count(), page);

        var viewModel = new List
        {
            Items = listItems.Skip((pager.CurrentPage - 1) * pager.PageSize).Take(pager.PageSize).ToList(),
            Pager = pager
        };

        return View(viewModel);
    }

我不确定我做的是否正确。但是现在,当我运行应用程序时,对于分页部分,它会在&#39; /&#39;中显示此消息&#39; 服务器错误应用程序 - 无法找到资源&#39;。我是否需要配置其他任何内容,并期望专家提出建议。感谢。

1 个答案:

答案 0 :(得分:1)

为了使分页的路由规则有效,您需要定义不同的操作。

例如:

routes.MapRoute( //Route For The Pagination
    "Home",
    "Home/page/{page}",
    defaults: new { controller = "Home", action = "PagedIndex" }
    );

然后,您在HomeController PagedIndex内部实施public ActionResult PagedIndex(int? page) { // code here to build your model return View("Index", viewModel); } 行动。

return View(viewModel);

您还需要将return View("Index", viewModel);更改为PagedIndex。否则,它将尝试运行public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( //Route For The Pagination "Home", "Home/page/{page}", defaults: new { controller = "Home", action = "PagedIndex" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } 视图。

修改:添加整个文件:

<强> App_Start / RouteConfig.cs

    public ActionResult Index()
    {
        Models.Home.Index model = new Models.Home.Index();

        return View("Index", model);
    }

    public ActionResult PagedIndex(int? page)
    {
        Models.Home.Index model = new Models.Home.Index();
        model.page = page ?? 0;

        return View("Index", model);
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

<强>控制器/ HomeController.cs

// You won't need this, it is just a mock model I created to test the code
namespace SOQuestion.Models.Home
{
    public class Index
    {
        public int page { get; set; }

        public Index()
        {

        }
    }
}

<强>模型/ Index.cs

@model SOQuestion.Models.Home.Index

@{
    ViewBag.Title = "Home Page";
}

<div class="row">
    <div class="col-md-12">
        <h2>This is the <b>Index</b> view</h2>
        <p>
            Current index is: <b>@Model.page</b>
        </p>
        <p>
            @if (Model.page > 0)
            {
                <a class="btn btn-info" href="/Home/Page/@(Model.page - 1)">Previous</a>
            }

            <a class="btn btn-info" href="/Home/Page/@(Model.page + 1)">Next</a>
        </p>
    </div>
</div>

<强>查看/主页/ Index.cshtml

uwsgi