Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }
like image 276
Doug Chamberlain Avatar asked Jun 21 '12 13:06

Doug Chamberlain


People also ask

How can we detect that a MVC controller is called by post or get?

You can check the Request. HttpMethod property. Save this answer.

What is post method in MVC?

Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...

What is PagedList MVC?

Pagination is a common requirement in a web application when it comes to show records from the databases or a static source. here we are going to do the pagination in MVC using PagedList nuget package. First create the MVC project and add a controller method to display the data records.


1 Answers

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;
    });
});
like image 114
Darin Dimitrov Avatar answered Sep 22 '22 03:09

Darin Dimitrov