20 tips for ASP. net mvc 3 Development (9) [20 recipes for programming MVC 3]: filter the list

Source: Internet
Author: User
Tags sql injection prevention actionlink

Topics

When sorting and paging cannot help users find the results they need, they can filter the content based on the conditions.

Solution

Adding a new link allows users to select pre-fabricated standard content to filter the list content through the LINQ library.

Discussion

To add a filter link, you must modify the books view, index view, and bookcontroller. The modification is similar to the previous two tips. To add an HTML link, the content must be content that can be selected by the user. Add three new links: all, new release, and forthcoming release. The newly released content is the content released in the last two weeks. The content to be released is the content to be released in the future.

Below are the new books and index views. The three links contain the parameters of the sorting rule (to maintain the current user's sorting options). The last two links contain a new variable parameter named "filter. Similar to the paging link, the active filter only displays links. inactive display text links are used to identify the selected filter options. To retain the options of the Current Filter when you change the sorting options, you also need to update the options according to the current filter:

@ Model pagedlist. ipagedlist <mvcapplication4.models. Book>

<H2> @ mvcapplication4.resources. resource1.bookindextitle </H2>
<P>
@ Html. actionlink ( " Create new " , " Create " )
</P>
<P>
Show:
@ If (viewbag. currentfilter! = "" )
{
@ Html. actionlink ( " All " ," Index " , New {
Sortorder = viewbag. currentsortorder })
}
Else
{
@: All
}
& Nbsp; | & nbsp;
@ If (viewbag. currentfilter! = " Newreleases " )
{
@ Html. actionlink (" New releases " , " Index " , New {
Filter = " Newreleases " , Sortorder =
Viewbag. currentsortorder })
}
Else
{
@: New releases
}
& Nbsp; | & nbsp;
@ If (viewbag. currentfilter! = " Comingsoon " )
{
@ Html. actionlink ( " Coming soon " , " Index " , New {
Filter = " Comingsoon " , Sortorder =
Viewbag. currentsortorder })
}
Else
{
@: Coming soon
}
</P>
@ Html. Partial ( " _ Paging " )
<Table>
<Tr>
<TH>
@ Html. actionlink ( " Title " , " Index " , New {
Sortorder = viewbag. titlesortparam,
Filter = viewbag. currentfilter })
</Th>
<TH>
@ Html. actionlink ( " ISBN " , " Index " ,New {
Sortorder = viewbag. isbnsortparam,
Filter = viewbag. currentfilter })
</Th>
<TH>
Summary
</Th>
<TH>
@ Html. actionlink ( " Author " , " Index " , New {
Sortorder = viewbag. authorsortparam,
Filter = viewbag. currentfilter })
</Th>
<TH>
Thumbnail
</Th>
<TH>
@ Html. actionlink ( " Price " , " Index " , New {
Sortorder = viewbag. pricesortparam,
Filter = viewbag. currentfilter })
</Th>
<TH>
@ Html. actionlink ( " Published " , " Index " , New {
Sortorder = viewbag. publishedsortparam,
Filter = viewbag. currentfilter })
</Th>
<TH> </Th>
</Tr>
@ Foreach ( VaR Item In Model)
{
<Tr>
<TD>
@ Html. displayfor (modelitem => item. Title)
</TD>
<TD>
@ Html. displayfor (modelitem => item. ISBN)
</TD>
<TD>
@ Html. displayfor (modelitem => item. Summary)
</TD>
<TD>
@ Html. displayfor (modelitem => item. Author)
</TD>
<TD>
@ Html. displayfor (modelitem => item. thumbnail)
</TD>
<TD>
@ Html. displayfor (modelitem => item. Price)
</TD>
<TD>
@ Html. displayfor (modelitem => item. published)
</TD>
<TD>
@ Html. actionlink ( " Edit " ,
" Edit " , New {Id = item. ID}) |
@ Html. actionlink ( " Details " ,
" Details " , New {Id = item. ID}) |
@ Html. actionlink (" Delete " ,
" Delete " , New {Id = item. ID })
</TD>
</Tr>
}
</Table>
@ Html. Partial ( " _ Paging " )

In the segment view of the paging link we created earlier, we also need to make such changes. In the followingCodeThe current page number, sorting rule, and filter options must also be transferred in the four page links:

 <  P  > 
@ If (model. haspreviouspage)
{
@ Html. actionlink (" < <First "," Index ", new {
Page = 1,
Sortorder = Viewbag. currentsortorder,
Filter = Viewbag. currentfilter })

@ Html. Raw ("& nbsp ;");

@ Html. actionlink ("<Prev", "Index", new {
Page = Model. pagenumber -1,
Sortorder = Viewbag. currentsortorder,
Filter = Viewbag. currentfilter })
}
Else
{
@: <First
@ Html. Raw ("& nbsp ;");
@: <Prev
}
& Nbsp;
@ If (model. hasnextpage)
{
@ Html. actionlink ("next > "," Index ", new {
Page = model. pagenumber + 1,
Sortorder = viewbag. currentsortorder,
Filter = viewbag. currentfilter })

@ Html. Raw (" & Nbsp; ");

@ Html. actionlink ("Last>", "Index", new {
Page = model. pagecount,
Sortorder = viewbag. currentsortorder,
Filter = viewbag. currentfilter })
}
Else
{
@: Next>
@ Html. Raw (" & Nbsp; ")
@: Last>
}
</ P >

Next, modify the index () method in bookscontroller. Accepting a new filter variable reduces the number of book lists based on the selected filter. There are two implementation methods:

    1. Select a condition for adding a Where string to LINQ;
    2. Use the strong-type selection condition of standard LINQ.

Because the filter usually does not contain too many entries, we will use the second method in this secret. When using the second method, because the entries are pre-fabricated and strong types are not dynamic, no additional input check or SQL Injection prevention is required.

 Using System;
Using System. Collections. Generic;
Using System. Data;
Using System. Data. entity;
Using System. LINQ;
Using System. LINQ. Dynamic;
Using System. Web;
Using System. Web. MVC;
Using Mvcapplication4.models;
Using Mvcapplication4.utils;
Using Pagedlist;

Namespace Mvcapplication4.controllers
{
Public Class Bookscontroller: Controller
{
Private Bookdbcontext DB = New Bookdbcontext ();
//
// Get:/books/

Public Viewresult index ( String Sortorder,
String Filter, Int Page = 1 )
{
# Region Viewbag Resources
...
# Endregion
# Region Viewbag sort Params
...
# Endregion
VaR Books = From B In DB. Books Select B;
# Region Filter Switch
Switch (Filter)
{
Case " Newreleases " :
VaR Startdate = datetime. Today. adddays (- 14 );
Books = books. Where (B => B. Published
<= Datetime. Today. Date
& Amp; B. Published & gt; = startdate
);
Break ;
Case " Comingsoon " :
Books = books. Where (B => B. Published>
Datetime. Today. date );
Break ;
Default :
// No filter needed
Break ;
}
Viewbag. currentfilter =
String. isnullorempty (filter )? "" : Filter;
# Endregion
Books = books. orderby (sortorder );
Int Maxrecords = 1 ;
Int Currentpage = page- 1 ;
Return View (books. topagedlist (currentpage,
Maxrecords ));
}
...
}

}

In the above example, you can filter the latest content and search for books published today or the past 14 days, or you can choose to publish and search for any books published today. Otherwise, all books will be returned if no filter is selected.

Reference

Original book Address Book source code

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.