如: aspx前台 这样写生成页面时不会产生新的html标签,用控件则会产生新的html标签
<h1><%= title %></h1>
<p><%= content %></p>
<ul>
<%
foreach (string item in list)
{
%>
<li><%= item %></li>
<%
}
%>
</ul>
获取一张表
<table>
<tr><th>ID</th><th>Name</th><th>Pwd</th><th>Sex</th></tr>
<%
foreach (Users item in arry)
{
%>
<tr><td><%=item.ID%></td><td><%=item.UserName%></td><td><%=item.UserPwd%></td><td><%=item.Sex%></td></tr>
<%
}
%>
</table>
//简单写法
foreach (Users item in dc.Users) /
{
%>
<tr><td><%=item.ID%></td><td><%=item.UserName%></td><td><%=item.UserPwd%></td><td><%=item.Sex%></td></tr>
<%
}
后台
public string title; //给h1填写类容 public string content; //给前台p标签添加类容 public string[] list; //给ul里面的li添加类容 public NorthwindDBDataContext dc; public List<Users> arry; //所有用户的集合 protected void Page_Load(object sender, EventArgs e) { dc = new NorthwindDBDataContext(); new List<Users>(); title = "MVC4"; content = "欢迎来到MVC新手教程哦!!!"; list = new string[] { "A", "B", "C", "D", "E" }; var result = from u in dc.Users select u; foreach (Users item in result) { arry.Add(item); //把所有的用户添加到arry集合里面 } }