是否有使用POST而不是得到一个MVC寻呼机? [英] Is there an MVC Pager that uses POST instead of GET?
问题描述
下面是我的问题。我有一个 SearchViewModel
有大量的搜索条件,值根本不适合在URL中。我目前使用特洛伊古德的Html.PagedListPager
,但它被设计为使用 Url.Action()
发送在URL中的参数。下面是一个例子。我不认为客户端筛选是一个选项,因为事实上我有很多的记录。
@ Html.PagedListPager(
(IPagedList)@ Model.SearchResults,
页=> Url.Action(结果,
新{
YearBuiltFrom = Model.YearBuiltFrom,
}
))
}
这是一个很好的解决方案,如果你只有一个或两个简单的参数。
SearchViewModel
公共类SearchViewModel
{ 公众诠释?页面{搞定;组; }
公众诠释?大小{搞定;组; } [IgnoreDataMember]
公共IPagedList<性> {SearchResult所获取;组; } 公共字符串[] {地点获得;组; } [IgnoreDataMember]
公共MultiSelectList LocationOptions {搞定;组; }
公共字符串[]邮编$ C $ {CS获得;组; } [IgnoreDataMember]
公共MultiSelectList邮编$ C $ {cOptions获得;组; }
[(建造年份NAME =)显示]
公众诠释? YearBuiltFrom {搞定;组; } [(建造年份NAME =)显示]
公众诠释? YearBuiltTo {搞定;组; }
公众诠释? SqftFrom {搞定;组; }
公众诠释? SqftTo {搞定;组; }
公共字符串卧室{搞定;组; }
公共字符串浴室{搞定;组; }
[数据类型(DataType.Date)
公众的DateTime? SalesFrom {搞定;组; }
[数据类型(DataType.Date)
公众的DateTime? SalesTo {搞定;组; }
公众诠释? SaleAmountFrom {搞定;组; }
公众诠释? SaleAmountTo {搞定;组; }
公众诠释? LandAreaFrom {搞定;组; }
公众诠释? LandAreaTo {搞定;组; } 公共字符串[] {海滨搞定;组; } [IgnoreDataMember]
公共MultiSelectList WaterfrontOptions {搞定;组; } // TODO:实现LandAreaType作为搜索参数
//公共字符串LandAreaType {搞定;组; }
公共布尔? IsVacant {搞定;组; } 公共字符串[] {PropertyFeatures获得;组; } [IgnoreDataMember]
公共MultiSelectList PropertyFeatureOptions {搞定;组; } }
我不熟悉这样的控制。我认为,最简单的是使用JavaScript来劫持的寻呼机锚点击通过取消将由锚引起的默认重定向动态地构建一个POST请求。要构建这个POST请求,你可以动态设置当前页面值到搜索表单的隐藏字段,并再次触发提交此表,以便它执行搜索,但与页面参数改变。
让我们看一个例子:
<! - 搜索表单包含所有搜索条件字段包括当前页码
@using(Html.BeginForm(NULL,NULL,FormMethod.Post,新{ID =searchForm}))
{
@ Html.EditorFor(X => x.SearchCriteria)
<按钮式=提交>搜索和LT; /按钮>
}<! - 这里会显示结果
< DIV ID =结果>
@ Html.DisplayFor(X => x.SearchResults)
< / DIV>
现在我们可以认购寻呼机上的单击事件:
$(函数(){
$('#结果')。点击(函数(){
//获取网页链接的URL
VAR URL = this.href; VAR页= ...提取网页链接的页面参数 //更新搜索表单里面的隐藏字段与此值
$('#页)VAL(页)。 //触发搜索
$('#searchForm)提交()。 //从导航到它指向的URL阻止链接
返回false;
});
});
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而不是得到一个MVC寻呼机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!