传递给操作的MVC分页过滤器视图模型参数

时间:2015-03-03 09:43:38

标签: ajax asp.net-mvc parameters paging

我正在开发一个搜索功能,其中包含使用过滤器视图模型传递的选项:

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并查看传递给操作的模型参数。

谢谢!

1 个答案:

答案 0 :(得分:1)

MvcPaging为我工作。我按照例子说。

在过滤视图模型中,我添加了&#39; 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; }

        ...

    }

然后我创建了一个视图模型,以便将搜索到的结果项保留在IPagedList和过滤器选项中:

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-s)。

@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>

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