c# - 在 Asp.net MVC 和 Entity Framework 中分页时如何应用过滤器?

标签 c# asp.net-mvc entity-framework asp.net-mvc-5 pagedlist

我有一个使用 ASP.NET MVC 框架编写的网络应用程序。在我的 Homecontroller 中,我有一个名为 Index 的操作,它响应 Get 请求。在此操作中,我使用 IPagedList 库创建页面以将记录分成多个页面。我的 Index@HttpGet 看起来像这样

public ActionResult Index(int? id)
{
    using(var connection = new Context())
    {
        int pageNumber = (id ?? 1);
        var presenter = new Presenter
        {
            Presenter = pageNumber,
            Tasks = connection.Tasks.ToPagedList(pageNumber, 30),
            Form = new TasksFiltersViewModel()
        }

        return View(presenter);
    }
}

我还有一个名为 Index 的操作,它响应应用了一些过滤器的 Post 请求。所以在 Post 请求中我做了这样的事情

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(Presenter model)
{
    int pageNumber = (id ?? 1);
    if (ModelState.IsValid)
    {
        using(var connection = new Context())
        {
            model.Tasks = connection.Tasks
                                    .Where(task => task.Status == 5)
                                    .ToPagedList(pageNumber, 30);
        }
    }

    return View(model);
}

这也能正常工作,除非用户更改了页面然后过滤器就停止了。

这是我的演示类的样子

public class Presenter
{
    public IPagedList<Task> Tasks { get; set; }
    public TasksFiltersViewModel Form { get; set; }
    public int PageNumber { get; set; }
    public IEnumerable<SelectListItem> Statuses { get; set; }
}

如何在保留过滤器的同时允许用户使用页面?

这是我的过滤器虚拟机

public class TasksFiltersViewModel
{
    public int Status { get; set; }
}

View 看起来像这样

@using (Html.BeginForm("Index", "Tasks", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        @Html.LabelFor(m => m.Form.Status, new { @class = "control-label col-sm-3" })
        <div class="col-sm-9">
            @Html.DropDownListFor(m => m.Form.Status, Model.Statuses, new { @class = "form-control" })
            @Html.ValidationMessageFor(m => m.Form.Status, "", new { @class = "text-danger" })
        </div>

    </div>

    <div class="row">
        <div class="col-sm-9 col-md-push-3">
            <div>
                <button type="submit" class="btn btn-default">Filter</button>
            </div>
        </div>
    </div>

}

foreach (var task in Model.Tasks)
{
    <tr>
        <td>@task.Name</td>
        <td>@task.Type</td>
        <td>@Html.ActionLink("Edit", "Details", "Task", new { @id = task.Id }, new { @class = "btn btn-primary btn-sm" })</td>
    </tr>
}


@Html.PagedListPager(Model.Tasks, id => Url.Action("Index", new { id }))

最佳答案

您的表单需要回发到 GET 方法,并且该方法需要包含过滤器属性的参数。 View 中的 PagedListPager 代码还需要包含这些过滤器属性,以便在您导航到下一页/上一页时保留它们。请注意,Index() POST 方法未使用,可以删除。

让您的模型包含过滤器属性的复杂对象以及绑定(bind)时的额外复杂性,因此首先将您的模型更改为

public class Presenter
{
    public IPagedList<Task> Tasks { get; set; }
    public int? Status { get; set; } // note nullable
    ... // add any other properties of TasksFiltersViewModel 
    public int PageNumber { get; set; }
    public IEnumerable<SelectListItem> Statuses { get; set; }
}

然后将Index()方法改为

public ActionResult Index(int? id, int? status) // add any other parameters your filtering on
{
    int pageNumber = (id ?? 1);
    var tasks = db.Tasks; // IQueryable<Task>
    if (status.HasValue)
    {
        tasks = tasks.Where(x => x.Status == status.Value)
    }
    if (otherParametersHaveValue)
    {
        tasks = tasks.Where(....);
    }
    Presenter model = new Presenter()
    {
        PageNumber = id ?? 1,
        Status = status,
        .... // set any other filter properties from the parameters
        Statuses = new SelectList(...),
        Tasks = tasks.ToPagedList(pageNumber, 30)
    };
    return View(model );
}

并将 View 更改为

// Make the form a GET
@using (Html.BeginForm("Index", "Tasks", FormMethod.Get, new { @class = "form-horizontal" }))
{
    ....
    // Modify expression based on revised model properties
    @Html.DropDownListFor(m => m.Status, Model.Statuses, ...)
}
....
// Add filter parameters to url so they are retained
@Html.PagedListPager(Model.Tasks, id => Url.Action("Index", new { id, status = Model.Status })) // add other filter properties as required

关于c# - 在 Asp.net MVC 和 Entity Framework 中分页时如何应用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42450257/

相关文章:

c# - 使用简单注入(inject)器注入(inject) MVC Controller 构造函数时未注册参数

c# - 数据层重构

c# - 如何在 ASP.Net Core 中实现自己的 DBContext 并仍然使用给定的 ApplicationUser 进行身份验证?

c# - 类型转换/类型转换性能

c# - 在 WebView (WinRT) 的外部浏览器中打开链接

c# - 从数据表中获取不同的字符串列表

c# - 如何使用 List<> 阻止 ToObservable?

css - MVC 捆绑和 CSS 相对 URL

.net - ASP.NET MVC 项目和 App_Code 文件夹

linq - Entity Framework 6 - 如果查询不包含任何行,则检索 null 而不是默认值