ASP.NET MVC实践系列6-Grid实现(上)

ASP.NET MVC中不推荐使用webform的控件了,也就是说当希望列表显示数据时不能使用GridView了,很多开源软件为ASP.NET MVC实现了列表的解决方案,这些具体的解决方案我们放到下节再说,这里介绍些简单的实现方式。

1、简单列表实现

参见:ASP.NET MVC实践系列2-简单应用

2、列表排序实现:

View代码:

Code
  <table>
        
<tr>
            
<th>
                ID
            
</th>
            
<th>
                
<%=Html.ActionLink("作者","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("作者","SortDemo",new{desc = Convert.ToBoolean(ViewData["desc"]),sortName="Author"}) %>输出到页面的html为: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));
            }
        }

同时还需要在这个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);
    }
}


上面的代码是用于动态拼接OrderBy的表达式的,当然我们也可以使用微软提供的Dynamic类,这个Dynamic类可以在\Microsoft Visual Studio 9.0\Samples\2052\CSharpSamples.zip的文件中的LinqSamples/DynamicQuery文件夹中找到。

3、列表翻页:

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)
                        {
                
%>
                
<%=i+1%>
                
<%          
                    }
                        
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、源码下载

5、参考:

微软的Dynamic

posted @ 2009-11-10 15:54  你听海是不是在笑  阅读(3257)  评论(3编辑  收藏  举报