c# - 是否有使用 POST 而不是 GET 的 MVC Pager?

标签 c# asp.net-mvc pagination

这是我的问题。我有一个具有大量搜索条件的 SearchViewModel,这些值根本不适合 URL。我目前正在使用 Troy Goode 的 Html.PagedListPager,但它被设计为使用 Url.Action() 来发送 URL 中的参数。这是一个例子。我不认为客户端过滤是一种选择,因为我会有很多记录。

 @Html.PagedListPager(
        (IPagedList)@Model.SearchResults,
        page => Url.Action("Results", 
            new {
                YearBuiltFrom = Model.YearBuiltFrom,
            }
                ))
}

如果您只有一两个简单的参数,这是一个很好的解决方案。

搜索 View 模型

  public class SearchViewModel
    {

        public int? page { get; set; }
        public int? size { get; set; }

        [IgnoreDataMember]
        public IPagedList<Property> SearchResults { get; set; }

        public string[] Locations { get; set; }

        [IgnoreDataMember]
        public MultiSelectList LocationOptions { get; set; }
        
        
        public string[] ZipCodes { get; set; }

        [IgnoreDataMember]
        public MultiSelectList ZipCodeOptions { get; set; }


        [Display(Name="Year Built")]
        public int? YearBuiltFrom  { get; set; }
     
        [Display(Name = "Year Built")]
        public int? YearBuiltTo { get; set; }
        public int? SqftFrom { get; set; }
        public int? SqftTo { get; set; }
        public string Bedrooms { get; set; }
        public string Bathrooms { get; set; }
        [DataType(DataType.Date)]
        public DateTime? SalesFrom { get; set; }
        [DataType(DataType.Date)]
        public DateTime? SalesTo { get; set; }
        public int? SaleAmountFrom { get; set; }
        public int? SaleAmountTo { get; set; }
        public int? LandAreaFrom { get; set; }
        public int? LandAreaTo { get; set; }
        
        public string[] Waterfront { get; set; }

        [IgnoreDataMember]
        public MultiSelectList WaterfrontOptions { get; set; }

     
        
        //TODO: Implement LandAreaType as a search parameter
        //public string LandAreaType { get; set; }
        public Boolean? IsVacant { get; set; }
        
        public string[] PropertyFeatures { get; set; }

        [IgnoreDataMember]
        public MultiSelectList PropertyFeatureOptions { get; set; }

    }

最佳答案

我对这样的控件不熟悉。我认为最简单的方法是使用 javascript 劫持寻呼机 anchor 上的点击,并通过取消由 anchor 引起的默认重定向来动态构建 POST 请求。要构建此 POST 请求,您可以将当前页面值动态设置到搜索表单的隐藏字段中,并触发此表单的提交,以便它再次执行搜索,但页面参数已更改。

举个例子:

<!-- Search form containing all the search criteria fields including the current page number
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "searchForm" }))
{
    @Html.EditorFor(x => x.SearchCriteria)
    <button type="submit">Search</button>
}

<!-- Here will be displayed the results
<div id="results">
    @Html.DisplayFor(x => x.SearchResults)
</div>

现在我们可以订阅寻呼机上的点击事件了:

$(function() {
    $('#results a').click(function() {
        // get the url of the page link
        var url = this.href;

        var page = ... extract the page parameter from the page link

        // update a hidden field inside the search form with this value
        $('#page').val(page);

        // trigger the search
        $('#searchForm').submit();

        // stop the link from navigating to the url it is pointing to
        return false;
    });
});

关于c# - 是否有使用 POST 而不是 GET 的 MVC Pager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11139443/

相关文章:

c# - 统一和泛型

c# - 无法获取 DisplayAttribute 名称

c# - 了解事务范围超时

asp.net - "Too Many Characters in String literal"消息来自哪里?

c# - Entity Framework /Azure Web服务模型设计

c# - 通过方法调用封装C#列表

javascript - Dropzone.js - 显示存储在 app_data 文件夹中的图像

php - Laravel 5 分页尾部斜杠重定向到 301

java - Spring mvc 分页

php - 在laravel中组合两个不同的无关系数据库表查询进行分页