是否有使用POST而不是GET的MVC寻呼机?

时间:2012-06-21 13:37:24

标签: c# asp.net-mvc pagination

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

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

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

SearchViewModel

  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; }

    }

1 个答案:

答案 0 :(得分:10)

我不熟悉这样的控制。我认为最简单的方法是使用javascript来劫持寻呼机锚点上的点击,并通过取消锚点引起的默认重定向来动态构建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;
    });
});