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可見得命名空間下有如下代碼:
Codepublic?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:
Codepublic?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
轉載于:https://www.cnblogs.com/nuaalfm/archive/2009/11/10/1596864.html
總結
以上是生活随笔為你收集整理的ASP.NET MVC实践系列6-Grid实现(上)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dubbo-admin管理平台搭建
- 下一篇: WPF 开发流程