HTML表中的MVC分页 [英] MVC pagination in html table

查看:61
本文介绍了HTML表中的MVC分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有搜索按钮的搜索表单.当用户输入所有搜索输入字段并按搜索按钮时,将仅返回同一页面html表中的前10条记录.直到做到这一点.

I have a search form with search button. When user enters all the search input fields and press the search button will return only top 10 records in the html table in the same page. I have done until this.

当用户按下下一页按钮时,我必须保留用户输入的值并将其发送到数据库并获取下10条记录.

When user press the next page button i have to retain the values entered by user and send it to the database and get next 10 records.

当我按下搜索按钮(第一篇文章)时,视图模型具有用户输入的所有值,但是当我按下下一页按钮单击(第二篇文章)时,视图模型未保留用户输入的值在搜索输入字段中.

When I press the search button (first http post) the view model has all the values entered by the user but when I press the next page button click(second post) the view model is not retaining the values entered by the user in the search input fields.

建议我不要使用临时数据将值从一个动作传递到另一个动作.还有其他方法吗?

I was advised to NOT to use temp data to pass values from one action to another action. Is there any other way to do this?

推荐答案

您是否考虑过分页的PagedList库?您要做的就是在ASP.NET MVC应用程序中引用PagedList库

Have you considered PagedList library for pagination? All what you have to do is to reference PagedList library in your ASP.NET MVC application

要安装PagedList.Mvc,请在程序包管理器控制台中运行以下命令.您也可以使用NuGet来获取此软件包.

To install PagedList.Mvc, run the following command in the Package Manager Console. You can use NuGet to get this package too.

PM> Install-Package PagedList.Mvc

您的视图模型

public class QuestionViewModel
{
        public int QuestionId { get; set; }
        public string QuestionName { get; set; }
}

在您的控制器中,引用PagedList

In your controller, reference PagedList

using PagedList;
and the Index method of your controller will be something like

public ActionResult Index(int? page)
{
            var questions = new[] {
                new QuestionViewModel { QuestionId = 1, QuestionName = "Question 1" },
                new QuestionViewModel { QuestionId = 1, QuestionName = "Question 2" },
                new QuestionViewModel { QuestionId = 1, QuestionName = "Question 3" },
                new QuestionViewModel { QuestionId = 1, QuestionName = "Question 4" }
            };

            int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(questions.ToPagedList(pageNumber, pageSize));
}

您的索引视图

@model PagedList.IPagedList<ViewModel.QuestionViewModel>
@using PagedList.Mvc; 
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />


<table>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionName)
        </td>
    </tr>
}

</table>

<br />

Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager( Model, page => Url.Action("Index", new { page }) )

结果屏幕看起来像

这篇关于HTML表中的MVC分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆