ASP. net mvc Practice Series 6-Grid implementation (I)

Source: Internet
Author: User
Tags actionlink

ASP. net mvc does not recommend the use of webform controls, that is, when you want the list to display data, you cannot use the GridView, many open source software is ASP. net mvc implements a list of solutions. The specific solutions are put in the next section. Here we will introduce some simple implementation methods.

1. Simple List Implementation

See ASP. net mvc practice series 2-simple applications

2. List sorting implementation:

View code:

Code
<Table>
<Tr>
<Th>
ID
</Th>
<Th>
<% = Html. ActionLink ("Author", "SortDemo", new {desc = Convert. ToBoolean (ViewData ["desc"]), sortName = "Author"}) %>
</Th>
<Th>
Title
</Th>
<Th>
CreateTime
</Th>
</Tr>
<% Foreach (var item in Model)
{%>
<Tr>
<Td>
<% = Html. Encode (item. ID) %>
</Td>
<Td>
<% = Html. Encode (item. Author) %>
</Td>
<Td>
<% = Html. Encode (item. Title) %>
</Td>
<Td>
<% = Html. Encode (String. Format ("{0: g}", item. CreateTime) %>
</Td>
</Tr>
<% }%>
</Table>
<% = Html. actionLink ("author", "SortDemo", new {desc = Convert. toBoolean (ViewData ["desc"]), sortName = "Author"}) %> the html output to the page is: http: // localhost: 4598 /? Desc = False & sortName = Author

Controller:

Code
Public ActionResult SortDemo (bool? Desc, string sortName)
{

List <News> list = ListNews. GetList ();
ParameterExpression p = Expression. Parameter (typeof (News), "p ");
MemberExpression expM;
System. Reflection. PropertyInfo propertyInfo;
If (string. IsNullOrEmpty (sortName ))
{
PropertyInfo = typeof (News). GetProperty ("ID ");
}
Else
{
PropertyInfo = typeof (News). GetProperty (sortName );

}
ExpM = Expression. MakeMemberAccess (p, propertyInfo );
Expression exp = Expression. Lambda (expM, p );
If (desc = null | desc = false)
{
ViewData ["desc"] = true;
Return View (list. AsQueryable <News> (). OrderBy (exp, true, propertyInfo. PropertyType ));
}
Else
{
ViewData ["desc"] = false;
Return View (list. AsQueryable <News> (). OrderBy (exp, false, propertyInfo. PropertyType ));
}
}
The following code also needs to be available in the namespace visible to the Controller:

Code
Public static class Dynamic
{

Public static IQueryable OrderBy (this IQueryable source, Expression ordering, bool desc, Type returnType)
{
Expression queryExpr = source. Expression;
QueryExpr = Expression. Call (typeof (Queryable), desc? "OrderBy": "OrderByDescending ",
New Type [] {source. ElementType, returnType },
QueryExpr, Expression. Quote (ordering ));
Return source. Provider. CreateQuery (queryExpr );
}
}

The above code is used to dynamically splice the OrderBy expression. Of course, we can also use the Dynamic class provided by Microsoft, this Dynamic class can be found in the LinqSamples/DynamicQuery folder in the \ Microsoft Visual Studio 9.0 \ Samples \ 2052 \ CSharpSamples.zip file.

3. List page flip:

View:

Code
<Table>
<Tr>
<Th>
ID
</Th>
<Th>
Author
</Th>
<Th>
Title
</Th>
<Th>
CreateTime
</Th>
</Tr>
<% Foreach (var item in Model)
{%>
<Tr>
<Td>
<% = Html. Encode (item. ID) %>
</Td>
<Td>
<% = Html. Encode (item. Author) %>
</Td>
<Td>
<% = Html. Encode (item. Title) %>
</Td>
<Td>
<% = Html. Encode (String. Format ("{0: g}", item. CreateTime) %>
</Td>
</Tr>
<% }%>
<Tr>
<Td colspan = "4" align = "right">
<%
Var currentPage = (int) ViewData ["currentPage"];
Var pages = (int) ViewData ["pages"];
For (int I = 0; I <pages; I ++)
{
If (currentPage = I)
{
%>
& Lt; % = I + 1% & gt;
<%
}
Else
{
%>
<% = Html. ActionLink (I + 1). ToString (), "NewsPageList", new {page = I}) %>
<%
}
%>
<%}
%>
</Td>
</Tr>
</Table>

Controller:

Code
Public ActionResult NewsPageList (int? Page)
{
List <News> list = ListNews. GetList ();
Const int pageSize = 5;
ViewData ["currentPage"] = page ?? 0;
ViewData ["pages"] = Convert. ToInt32 (Math. Ceiling (double) list. Count ()/pageSize ));

Var news = list. Skip (page ?? 0) * pageSize). Take (pageSize );
Return View (news );
}

4. Download source code

5. Reference:

Microsoft Dynamic

Article from: http://www.cnblogs.com/nuaalfm/

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.