Mvc5+ef6 Getting Started complete tutorial VII

Source: Internet
Author: User

Original: Mvc5+ef6 Complete Tutorial VII

In this article we add some new features for the table display.

We've already explained the data in the table before, and now we've added three common features:

Sort, filter, and page the displayed results.

article outline
    • Theoretical basis/pre-preparation
    • Detailed steps
    • Summarize
Pre-Preparation – add test data by applying previous styles

our views on the previous à Account à index.cshtml is modified to complete today's example.

Before the interface style is modified:

under the face of views à Account à index.cshtml make the following modifications:

    1. Apply Layout page _layoutadmin.cshtml

2. Delete all except the body of the HTML part, leaving only the text content, run this page.

Make some small adjustments to the style, and eventually become the following style.

(Adjust the style of the steps slightly, you can directly view the source code)

Create new two test data with create new to prepare for the next pagination, followed by one page for each of the 3 bars.

At present the preparation work is OK, below starts today the function operation.

Detailed Steps

First, the title Added link sorting function

    1. Open Controllers à AccountController.cs, modify the index method as follows

2. Openviewsàaccountàindex.cshtml, replace the title content.

@ Html.ActionLink ("UserName", "Index", new {sortOrder = viewbag.namesortparm})

Click on the title to switch between ascending and descending order.

Second, add the name search function

Continue to modify the index method to increase the conditional filtering feature. The part of the change is shown in the yellow background.

We added a searchstring parameter and added a WHERE clause to filter the name.

now we'll go to the views à Account à Add a text box in the index.cshtml to pass this filter value.

Now test it and run this page to see that the results match our expectations.

OK, here's the last function, paging.

Third, add paging function (using PAGEDLIST.MVC)

Installing PAGEDLIST.MVC

Open the package Manager Console to ensure that the package source is nuget.org, enter

Install-package PAGEDLIST.MVC

Add a declaration first in the AccountController.cs.

Using pagedlist;

Modify the Index method

The index is increased by the following highlight (I only intercept the beginning and end of the section)

We have added two incoming parameters CurrentFilter and page

All incoming values are null when the page is first displayed or when no page number or sort is clicked

When you click the paging link, the page number is passed in.

The current sort order needs to be passed in because the same order needs to be maintained when paging.

Viewbag.currentsort = SortOrder;

Another property, Viewbag.currentfilter, provides the current filter string for the view.

This value is used to maintain the filter condition unchanged when paging, and must be re-displayed in the text box when paging. If the filter condition changes when the child is paged, the page must be reset to 1 because the new filter condition causes different data to be displayed. SearchString is not NULL when filter conditions change

In the end, Topagedlist passes a collection of Query results transformation component pages to the view.

Note

?? The operator is called null-coalescing operator.

This operator defines a default value for nullable types. (page?? 1) indicates that if page is null returns 1, the value of the page is returned.

Let's look directly at the following example:

Add pagination display and feature links in view

Main additions/Modifications to the following sections:

The @model declaration at the top specifies that view obtains a Pagedlist object.

(It's a list object, see the Notes section)

Add @using Pagedlist.mvc, get related paging helper

Overloaded BeginForm is get mode.

As we mentioned earlier in this article, if you do not involve updating data operations, we recommend the Get method.

The text box is initialized to CurrentFilter.

This allows you to see the current filter condition when you switch to the next page.

The title link of the table also passes the currentfilter over.

Finally, add the current number of pages and the total quantity similar to page x in x format.

Displays page 0 of 0 if no pages are displayed

In this case, page number>page count.

According to the index method, it can be learned that PageNumber is 1,model.pagecount 0.

The paging button is displayed by Pagedlistpager Helper.

Final result

Second page

Legacy issues

Finally did an experiment, found that there is a problem, know the park friends to help answer under.

It's no problem sorting username.

When I sort by email, if I add the condition directly to the expression, it's OK, like.

Reverse row

If you do this in the following way

Or

The following results appear in both the positive and reverse order:

Summary of the problem, the following two kinds of writing, the second does not meet the expectations.

Summary

This time we mainly explained the sorting, filtering conditions, paging three functions.

In addition, there is a left problem, know the garden friends under the solution.

This time the knowledge point is more broken, detailed can refer to the code.

Please be fully aware of a few examples in this article.

All right, here we go today.

Welcome everyone to comment, let the next article better:)

Mvc5+ef6 Getting Started complete tutorial VII

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.