ASP. net mvc series: Use Flurl to create reusable paging components, mvcflurl

Source: Internet
Author: User

ASP. net mvc series: Use Flurl to create reusable paging components, mvcflurl

Use ASP.. net mvc query, the MvcPaging component is always used, although you need to customize MvcPaging. pager can achieve the desired effect, but it is still used when there is no better URL library. The paging logic is not complex, and more importantly, it has been discovered recently.FlurlThis open-source library uses Flurl to create a DIY paging component. The core is the PagingModel file. The algorithm may not be optimal, but it contains the main logic and implementation key points. In terms of query, I use the following practices:

1. encapsulate query parameters using ViewModel

2. Use ViewModel as the Model type of View

3. Submit and query the Action using the form GET method.

4. directly set Url parameters to submit and query the Action

Therefore, self-made paging components are also applied in the above environment. Originally, the query and paging are written together, but the paging part is independent to facilitate independent demonstration.

1. First, use the String extension method to wrap the Flurl method so that other libraries can be replaced later.

2. Customize the PagingModel object inherited from IHtmlString, so that it does not depend on the MVC library. At the same time, the IvalidatableObject interface is implemented to check for PageSize exceptions. Some default parameters can be configured to be read from the configuration file (in the ASP. net mvc series: File Upload Demo with relevant Code) or the configuration database.

3. Use or use the HtmlHelper Extension Method to wrap the call directly in ASP. net mvc Razor. The class library can be guaranteed again by referencing ASP. net mvc, so that the SelectList object can be directly returned.

Action:

View:

4. Instead of inheriting from PagingModel, you can directly add a PagingModel Method to the View, for example:

5. Demo:

(1) If the number of links close to the home page and the last page is insufficient, it will be supplemented in the middle. In the blog Garden, the navigation is only processed near the home page, and the last page is not correctly processed.

(2) The default PageIndex and PageSize parameters are not added. When the Url parameter is null, it is automatically filtered out.

Demo: Click to download

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.