All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
C#中索引器的概念和应用实例
将输入的20041219等的数字字符串等格式化成日期型_[Asp.Net教程]
PHP5.2+APACHE2.2+BugFree1.0的安装_[PHP教程]
ASP.NET实例:利用对象序列化将购物车保存在Cookie中_[Asp.Net教程]
加速你的.NET程序_[Asp.Net教程]
ASP.NET程序上传文件功能的具体实例代码_.net资料_编程技术
C#教程:向注册表写入信息
PHP分页实例:用PHP实现网页开发中的翻页跳转_[PHP教程]
轻松掌握Ajax.net系列教程九:使用Accordion_[Asp.Net教程]
ASP.NET实现下拉框二级联动组件_[Asp.Net教程]
Reading number is top 10 pictures
China telecom 114 spokesman MeiYanXu1
大四女生借债隆胸成功
Terrorist smile the largest human history an explosion1
青春清纯美女大集合1
Exquisite decoration is not paying too much3
Look for from human art net, is good--2
Ashlynn Brooke show proud chest measurement2
China's programmers are live what kind, had a look at will know that
Hunan province aizhai super-large suspension bridge open to traffic and 4 world first1
Beautiful Japanese beauty(漂亮的日本美女)2
Download software ranking
Boxer vs Yellow4
电车之狼R
Unix video tutorial2
apache-tomcat-6.0.33
Ashlynn Video4
小黑猫大战两米大花蛇
Boxer's Top ten classic battle6
I'm come from Beijing1
ASP.NET.2.0.XML.高级编程(第3版)
双旗镇刀客A
归海一刀 published in(发表于) 2014/1/30 0:52:30 Edit(编辑)
ASP.NET,MVC,Framework体验(2):显示列表数据_[Asp.Net教程]

ASP.NET,MVC,Framework体验(2):显示列表数据_[Asp.Net教程]

ASP.NET MVC Framework体验(2):显示列表数据_[Asp.Net教程]


概述


ASP.NET WebForm下,显示列表数据,经常会使用服务器控件GridView、DataList等。在ASP.NET MVC Framework中,我们有两种方式进行显示数据,一是使用行内代码,即通过循环视图数据使用<%=%>标记进行呈现;二是使用服务器控件,同样可以把视图数据绑定在服务器控件,如ASP.NET 3.5中的新控件ListView。


准备数据访问


这里我们显示一个Post的列表DataContext和实体定义如下:

[Database(Name="Blog")]
public class BlogDataContext : DataContext
{
public BlogDataContext()
: base(@"Server=.\Sql2005;User Id=sa;Password=;Database=Blog")
{
}
public Table Posts
{
get
{
return this.GetTable();
}
}
}
Post实体:
[Table(Name="Posts")]
public class Post
{
[Column(IsPrimaryKey=true,IsDbGenerated = true)]
public int Id
{
get; set;
}
[Column]
public string Title
{
get; set;
}
[Column]
public string Author
{
get; set;
}
[Column]
public DateTime PubDate
{
get; set;
}
[Column]
public string Description
{
get; set;
}
}

同时,我们定义一个BlogRepository类,用于读取Post数据,这样可以使得Controller中代码更加优雅,不再涉及数据访问:

public class BlogRepository
{
public List GetAll()
{
BlogDataContext db = new BlogDataContext();
IEnumerable posts = from p in db.Posts
orderby p.PubDate
select p;
return posts.ToList();
}
}

定义Controller


这里的Controller定义就非常简单了,获取所有Post数据,然后把数据传给视图

public class BlogController : Controller
{
[ControllerAction]
public void Index()
{
// 获取所有post数据
BlogRepository repository = new BlogRepository();
List posts = repository.GetAll();
// 转向视图Index,显示Post列表
RenderView("Index", posts);
}
}

定义View


添加一个Index视图,并使其继承于ViewPage>。


1.使用行内代码显示,进行数据的循环并使用ViewPage提供的HtmlHelper方法。

1.使用行内代码


<%=Html.ActionLink("Home", new { action="Index"})%> |
<%foreach (Post post in ViewData)
{ %>
Title:<%=Html.Encode(post.Title) %>

Author:<%=Html.Encode(post.Author) %>

PubDate:<%=Html.Encode(post.PubDate.ToShortDateString()) %>

Content:<%=Html.Encode(post.Description) %>



<% } %>

在HTML代码中编写时VS2008同样提供了很好的智能提示功能:


TerryLee_MVC_004


2.使用服务器控件ListView,编写代码如下:

使用ListView控件







Title:<%# Eval("Title") %>
Author:<%# Eval("Author")%>

PubDate:<%# Eval("PubDate")%>

Content:<%# Eval("Description") %>





在后台代码中进行ListView的数据绑定,这里仅仅是对把视图数据绑定到了ListView上面,从数据库中获取数据交给Controller去做。

public partial class Views_Blog_Index : ViewPage>
{
protected void Page_Load(object sender, EventArgs e)
{
this.ListView1.DataSource = ViewData;
this.ListView1.DataBind();
}
}

设置路径选择


同样我们需要进行路径选择的设置

void Application_Start(object sender, EventArgs e) 
{
// Code that runs on application startup
RouteTable.Routes.Add(
new Route
{
Url = "[controller]/[action].mvc",
Defaults = new { action = "Index" },
RouteHandler = typeof(MvcRouteHandler)
});
}

完成后,运行可以看到,使用行内代码和ListView控件的效果是一样的


TerryLee_MVC_005


TerryLee_MVC_006


结束语


在文章结束时,顺便说一下,好多朋友都问为什么有了WebForm,还要再出一个ASP.NET MVC Framework,对于这个问题,建议大家阅读一下这篇文章What's Ailing ASP.NET Web Forms

示例代码下载:/Files/Terrylee/MVCDemo02.rar

作者:TerryLee
出处:http://terrylee.cnblogs.com






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.