日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2023/12/18 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

轉載于:https://www.cnblogs.com/nuaalfm/archive/2009/11/10/1596864.html

總結

以上是生活随笔為你收集整理的ASP.NET MVC实践系列6-Grid实现(上)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。