100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ASP.NET MVC实践系列6-Grid实现(上)

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

时间:2019-09-13 17:54:40

相关推荐

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

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

1、简单列表实现

参见: 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(variteminModel)

{%>

<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

publicActionResultSortDemo(bool?desc,stringsortName)

{

List<News>list=ListNews.GetList();

ParameterExpressionp=Expression.Parameter(typeof(News),"p");

MemberExpressionexpM;

System.Reflection.PropertyInfopropertyInfo;

if(string.IsNullOrEmpty(sortName))

{

propertyInfo=typeof(News).GetProperty("ID");

}

else

{

propertyInfo=typeof(News).GetProperty(sortName);

}

expM=Expression.MakeMemberAccess(p,propertyInfo);

Expressionexp=Expression.Lambda(expM,p);

if(desc==null||desc==false)

{

ViewData["desc"]=true;

returnView(list.AsQueryable<News>().OrderBy(exp,true,propertyInfo.PropertyType));

}

else

{

ViewData["desc"]=false;

returnView(list.AsQueryable<News>().OrderBy(exp,false,propertyInfo.PropertyType));

}

}

同时还需要在这个Controller可见得命名空间下有如下代码:

Code

publicstaticclassDynamic

{

publicstaticIQueryableOrderBy(thisIQueryablesource,Expressionordering,booldesc,TypereturnType)

{

ExpressionqueryExpr=source.Expression;

queryExpr=Expression.Call(typeof(Queryable),desc?"OrderBy":"OrderByDescending",

newType[]{source.ElementType,returnType},

queryExpr,Expression.Quote(ordering));

returnsource.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(variteminModel)

{%>

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

<tdcolspan="4"align="right">

<%

varcurrentPage=(int)ViewData["currentPage"];

varpages=(int)ViewData["pages"];

for(inti=0;i<pages;i++)

{

if(currentPage==i)

{

%>

<%=i+1%>

<%

}

else

{

%>

<%=Html.ActionLink((i+1).ToString(),"NewsPageList",new{page=i})%>

<%

}

%>

<%}

%>

</td>

</tr>

</table>

Controller:

Code

publicActionResultNewsPageList(int?page)

{

List<News>list=ListNews.GetList();

constintpageSize=5;

ViewData["currentPage"]=page??0;

ViewData["pages"]=Convert.ToInt32(Math.Ceiling((double)list.Count()/pageSize));

varnews=list.Skip((page??0)*pageSize).Take(pageSize);

returnView(news);

}

4、源码下载

5、参考:

微软的Dynamic

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。