Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

c# - Is there an MVC Pager that uses POST instead of GET?

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...