MVC Paging - 过滤传递给操作的视图模型参数

问题描述 投票:0回答:1

我正在开发搜索功能,其中的选项通过过滤器视图模型传递:

public class FilterViewModel
{
    public string UserName { get; set; }

    public int? TownId { get; set; }

    [Display(Name = "Gender:")]
    public bool? IsMale { get; set; }

    [Display(Name = "Interests:")]
    public int[] InterestsIds { get; set; }

    public List<ProfileInterest> Interests { get; set; }

    ...

}

该操作通过

Ajax
:

调用
[HttpPost]
public ActionResult FilterProfiles(FilterViewModel filter)
{
   //Gets the filtered items and returns a partial view     
   ...
}

有没有一个分页库来管理

Ajax
并查看传递给action的模型参数。

ajax asp.net-mvc parameters pagination
1个回答
1
投票

MvcPaging对我有用。我按照示例进行操作。

在过滤器视图模型中我添加了“Page”属性:

    public class FilterViewModel
    {
        public int? Page { get; set; }

        public string UserName { get; set; }

        public int? TownId { get; set; }

        [Display(Name = "Gender:")]
        public bool? IsMale { get; set; }

        [Display(Name = "Interests:")]
        public int[] InterestsIds { get; set; }

        ...

    }

然后我创建了一个视图模型来将搜索结果项和过滤器选项保留在 IPgedList 中:

public class CurrentFilterViewModel
{
    //keeps the search result items
    public IPagedList<ProfileViewModel> Profiles { get; set; }

    public string UserName { get; set; }

    public string LastName { get; set; }

    public int? TownId { get; set; }

    public bool? IsMale { get; set; }

    public int[] InterestsIds { get; set; }

    ...

}

在操作中,我将搜索结果项和过滤器属性值传递给当前模型属性

    [HttpPost]
    public ActionResult FilterProfiles(FilterViewModel filter)
    {
        var filteredProfilesModel = new CurrentFilterViewModel();
        filteredProfilesModel.UserName = filter.UserName ?? null;
        filteredProfilesModel.TownId = filter.TownId ?? null;
        filteredProfilesModel.IsMale = filter.IsMale ?? null;
        filteredProfilesModel.InterestsIds = filter.InterestsIds ?? new int[0];
        filteredProfilesModel.MusicGenresIds = filter.MusicGenresIds ?? new int[0];

        int DefaultPageSize = 3;
        int currentPageIndex = filter.Page.HasValue ? filter.Page.Value - 1 : 0;

        filteredProfilesModel.Profiles = 
        // ... gets the responded items from the database ...
        .ToPagedList(currentPageIndex, DefaultPageSize);

        return this.PartialView("_FilterProfilesPartial", filteredProfilesModel);
    }

至少,在我用ajax放置的分部视图中,我放置了分页:将过滤器参数值设置为当前模型对应的属性值。使用 AddRouteValueFor 设置集合参数(我使用数组来保存多个选定项目的 id)。

@using MvcPaging;
@using System.Linq;
@model Project.Web.ViewModels.CurrentFilterViewModel

    <div class="pager">
        @Html.Pager(Model.Profiles.PageSize, Model.Profiles.PageNumber, Model.Profiles.TotalItemCount,
        new AjaxOptions
               {
                   UpdateTargetId = "profiles",
                   HttpMethod = "POST"
               }).Options(o => o
                   .Action("FilterProfiles")
                   .AddRouteValueFor(m => m.InterestsIds)
                   .AddRouteValue("UserName", Model.FirstName)
                   .AddRouteValue("TownId", Model.TownId)
                   .AddRouteValue("IsMale", Model.IsMale)
                   )
    </div>

这个解决方案对我有用。我不确定它是否非常优雅。也许有更好的。

© www.soinside.com 2019 - 2024. All rights reserved.