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

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

1. 列表排序实现:

View代码:

<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>
        <% } %>

这里可能需要注意的就是<%=Html.ActionLink("作者","SortDemo",new{desc = Convert.ToBoolean(ViewData["desc"]),sortName="Author"}) %>输出到页面的html为:http://localhost:4598/?desc=False&sortName=Author

Controller:

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可见得命名空间下有如下代码:

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文件夹中找到。

2、列表翻页:

View:

<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:

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);
        }

3、源码下载
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值