ASP. net mvc Framework experience (2): display list data

Source: Internet
Author: User
Overview

In ASP. NET WebForm, list data is displayed, and server controls such as GridView and DataList are often used. In ASP. in the. net mvc Framework, data is displayed in two ways. One is to use in-row code, that is, to use the <% =%> mark to present the data in the circular view; second, you can also bind View data to server controls, such as ASP.. NET 3.5.

Prepare Data Access

The following figure shows the DataContext and object definitions of a Post list:

[Database(Name="Blog")]public class BlogDataContext : DataContext{    public BlogDataContext()        : base(@"Server=.\Sql2005;User Id=sa;Password=;Database=Blog")    {             }    public Table<Post> Posts    {        get        {            return this.GetTable<Post>();        }    }}
Post object:
[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;    }}

At the same time, we define a BlogRepository class for reading Post data, which makes the code in the Controller more elegant and does not involve data access:

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

The Controller definition here is very simple. Get all Post data and pass the data to the view.

Public class BlogController: Controller {[ControllerAction] public void Index () {// obtain all post data BlogRepository repository = new BlogRepository (); List <Post> posts = repository. getAll (); // turn to the View Index and display the Post list RenderView ("Index", posts );}}
Define View

Add an Index view and make it inherit from ViewPage <List <Post>.

1. Use in-line code display to loop data and use the HtmlHelper method provided by ViewPage.

<H3> 1. use in-line code 

When compiling HTML code, VS2008 also provides a good smart reminder function:

 

2. Use the Server Control ListView and write the code as follows:

<H3> use the ListView Control 

Bind the ListView data in the background code. Here we only bind the View data to the ListView, and obtain the data from the database to the Controller.

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

We also need to set the path selection.

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)                });}

After running, you can see that the effect of using the in-row code is the same as that of the ListView control.

Conclusion

At the end of the article, many friends asked why WebForm was available and another ASP. net mvc Framework. We recommend that you read this article. NET Web Forms.

Download the sample code:/Files/Terrylee/MVCDemo02.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.