请先阅读这里:
ASP.NET Framework Features That Are Compatible with MVC
It might appear that Web Forms and MVC are very different technologies. However, both of these technologies are built on the ASP.NET framework. Therefore, most ASP.NET framework features that you have used to create applications that are based on Web Forms are also available to you for developing MVC applications. This includes features such as membership, authentication, roles, and configuration, which work the same way in an MVC application as they do in a Web Forms application. Most ASP.NET namespaces, classes, and interfaces can be used in an ASP.NET MVC application.
ASP.NET Framework Features That Are Incompatible with MVC
Because ASP.NET MVC does not maintain state information by using view state, you must find other ways to manage state information, if you need it. In addition, server controls that rely on view state and postback will not work as designed in an ASP.NET MVC application. Therefore, you should not use controls such as the GridView, Repeater, and DataList controls.
源文档 <http://msdn.microsoft.com/en-us/library/dd381619.aspx>
准备工作:
public class Student
{
public int Number { get; set; }
public string Name { get; set; }
}
//Dummy Data构造的绑定数据如下:
List<Student> students = new List<Student>();
students.Add(new Student { Name = "lee", Number = 123 });
students.Add(new Student { Name = "Zen", Number = 234 });
students.Add(new Student { Name = "angle", Number = 101 });
ViewData["Students"] = students;
问题是什么?
在View中使用Repeater的问题其实就是绑定事件的执行,明确了这一点其实问题就很简单了.
方案一:页面级解决
我们设计一个View的基类,该基类在页面Load的时候完成DataBind,这样不管页面中有没有需要绑定的控件,需要绑定的控件是什么,这个方法执行完之后就会完成绑定.首先验证我们的想法:
public class AutoBindViewPage : ViewPage
{
protected override void OnLoad(EventArgs e)
{
this.DataBind();
base.OnLoad(e);
}
}
页面代码片段:

















注意:Inherits="AutoBindViewPage"
跑一下程序,成功.
原理:
我们看一下继承关系:AutoBindViewPage-->ViewPage-->Page-->TemplateControl-->Control
考察一下Control类的DataBind事件:
































关键就在这里:this.DataBindChildren();
方案二:AutoBindRepeater
扩展ViewPage不是什么好主意,把"Repeater绑定"看做一个职责,那么这个职责应该属于Repeater自己的.想到这里也就简单了,删除掉刚刚扩展的类,实现一个AutoBindRepeater:
{
{
protected override void OnLoad(EventArgs e)
{
this.DataBind();
base.OnLoad(e);
}
}
}
页面代码片段:
<%@ Register Namespace="TestControls" Assembly="ZenMvc" TagPrefix="My" %>
… …
<My:AutoBindRepeater DataSource='<%#ViewData["Students"]%>' runat="server">
<ItemTemplate>
Name:<%#Eval("Name")%><br />
</ItemTemplate>
</My:AutoBindRepeater>
跑一下,通过.
方案三:AutoBindRepeaterUseDataKey
方案三其实是做一点简单的改造,可以看到 DataSource='<%#ViewData["Students"]%>'在每一个Repeater中都是重复的,改造的目的就是为了简化这一语句.方法也很简单:添加对数据源的解析就可以.
{
private string _dataKey;
public string DataKey
{
get { return _dataKey; }
set { _dataKey = value; }
}
protected override void OnLoad(EventArgs e)
{
ViewPage viewPage = (System.Web.Mvc.ViewPage)Page;
this.DataSource = viewPage.ViewData[DataKey];
this.DataBind();
base.OnLoad(e);
}
}
页面代码片段:
<ItemTemplate>
Name:<%#Eval("Name")%>
Number:<%#Eval("Number") %><br />
</ItemTemplate>
</My:AutoBindRepeaterUseDataKey>
调试,通过.
方案四:MVCRepeater
细心地你一定发现了,在上面的改造中,我们已经在标准的Repeater实现中引入了MVC框架的内容.在MvcFutures项目中的Microsoft.Web.Mvc.Controls里面,我们找到了一个MVCRepeater的实现:
<MVC:Repeater Name="Students" runat="server" >
<ItemTemplate>
Name:<%#Eval("Name")%><br />
</ItemTemplate>
</MVC:Repeater>
MVCRepeater的实现略.
方案五:foreach语句
继续追究为什么要使用Repeater,是因为我们要展示一组数据,走到这里我们的调整一下目标,不再是在MVC使用Repeater控件,而是实现在asp.net MVC 中展示一组数据.
foreach语句具有相当好的表现力:
[Name]: <%=item.Name%><br/>
<%} %>
方案五:MvcContrib Grid & SparkViewEngine
全面考虑, MvcContrib Grid和SparkViewEngine也是具有技术可行性.但是是否要引入到项目中要谨慎考虑.
column.For(x => x.Id).Named("Person ID");
column.For(x => x.Name);
column.For(x => x.DateOfBirth).Format("{0:d}");
})
.Attributes(style => "width:100%")
.Empty("There are no people.")
.RowStart(row => "<tr foo='bar'>") %>
源文档 <http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/>
<ul if="products.Any()">
<li each="var p in products">${p.Name}</li>
</ul>
<else>
<p>No products available</p>
</else>
源文档 <http://sparkviewengine.com/>
嗯哼,全文完.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义