本文介绍了是否有使用 POST 而不是 GET 的 MVC Pager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的问题.我有一个具有大量搜索条件的 SearchViewModel
,这些值根本不适合 URL.我目前正在使用 Troy Goode 的 Html.PagedListPager
,但它旨在使用 Url.Action()
发送 URL 中的参数.这是一个例子.我不认为客户端过滤是一种选择,因为我会有很多记录.
@Html.PagedListPager((IPagedList)@Model.SearchResults,页面 =>Url.Action("结果",新的 {YearBuiltFrom = Model.YearBuiltFrom,}))}
如果您只有一两个简单参数,这是一个很好的解决方案.
搜索视图模型
公共类 SearchViewModel{公众号?页{得到;放;}公众号?大小{得到;放;}[忽略数据成员]公共 IPagedList搜索结果 { 得到;放;}公共字符串[]位置{获取;放;}[忽略数据成员]公共 MultiSelectList LocationOptions { 获取;放;}公共字符串 [] 邮政编码 { 获取;放;}[忽略数据成员]公共 MultiSelectList ZipCodeOptions { 获取;放;}[显示(名称=建造年份")]公众号?YearBuiltFrom { 得到;放;}[显示(名称=建造年份")]公众号?YearBuiltTo { 得到;放;}公众号?SqftFrom { 得到;放;}公众号?SqftTo { 得到;放;}公共字符串卧室{得到;放;}公共字符串浴室{得到;放;}[数据类型(数据类型.日期)]公共日期时间?销售从{得到;放;}[数据类型(数据类型.日期)]公共日期时间?SalesTo { 得到;放;}公众号?SaleAmountFrom { 得到;放;}公众号?SaleAmountTo { 得到;放;}公众号?LandAreaFrom { 得到;放;}公众号?LandAreaTo { 得到;放;}public string[] Waterfront { get;放;}[忽略数据成员]公共 MultiSelectList WaterfrontOptions { get;放;}//TODO: 实现 LandAreaType 作为搜索参数//公共字符串 LandAreaType { get;放;}公共布尔?IsVacant { 得到;放;}公共字符串[] PropertyFeatures { 获取;放;}[忽略数据成员]公共 MultiSelectList PropertyFeatureOptions { 获取;放;}}
解决方案
我对这样的控件并不熟悉.我认为最简单的方法是使用 javascript 来劫持对寻呼机锚点的点击,并通过取消由锚点引起的默认重定向来动态构建 POST 请求.要构建此 POST 请求,您可以将当前页面值动态设置到搜索表单的隐藏字段中并触发此表单的提交,以便它再次执行搜索,但页面参数已更改.
举个例子:
<!-- 包含所有搜索条件字段的搜索表单,包括当前页码@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "searchForm" })){@Html.EditorFor(x => x.SearchCriteria)<button type="submit">搜索</button>}<!-- 这里会显示结果<div id="结果">@Html.DisplayFor(x => x.SearchResults)
现在我们可以订阅传呼机上的点击事件:
$(function() {$('#results a').click(function() {//获取页面链接的urlvar url = this.href;var page = ... 从页面链接中提取页面参数//用这个值更新搜索表单中的隐藏字段$('#page').val(page);//触发搜索$('#searchForm').提交();//停止链接导航到它指向的 url返回假;});});
Here is my issue. I have a SearchViewModel
that has a large number of search criteria, the values simply won't fit in the URL. I'm currently using Troy Goode's Html.PagedListPager
, but it is designed to use Url.Action()
to send the parameters in the URL. Here is an example. I don't think client-side filtering is an option, due to the fact I'll have a lot of records.
@Html.PagedListPager(
(IPagedList)@Model.SearchResults,
page => Url.Action("Results",
new {
YearBuiltFrom = Model.YearBuiltFrom,
}
))
}
This is a fine solution if you only have one or two simple parameters.
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; }
}
解决方案
I am not familiar with such a control. I think that the easiest would be to use javascript to hijack the click on a pager anchor and build a POST request dynamically by cancelling the default redirect that will be caused by the anchor. To build this POST request you could dynamically set the current page value into a hidden field of the search form and trigger the submission of this form so that it performs the search again but with the page parameter changed.
Let's take an example:
<!-- 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>
now we could subscribe for the click event on the pager:
$(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;
});
});
这篇关于是否有使用 POST 而不是 GET 的 MVC Pager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!