Go to ASP. NET MVC display list data

Source: Internet
Author: User

Overview

Under ASP. NET WebForm, the list data is displayed, often using server controls such as GridView, DataList, and so on. In the ASP. NET MVC Framework, we have two ways to display the data, one is to use the inline code, that is, to render using the <%=%> tag through the loop view data, and the other is to use the server control to bind the view data to the server control, such as ASP. 3.5 new control in the ListView.

Defining a Controller

The controller definition here is very simple, get all the post data, and then pass the data to the view

[CSharp]View Plaincopy
  1. Public class Blogcontroller:controller
  2. {
  3. [Controlleraction]
  4. public void Index ()
  5. {
  6. //Get all post data
  7. Blogrepository repository = new Blogrepository ();
  8. List<post> posts = repository. GetAll ();
  9. //Turn to view index, show post list
  10. Renderview ("Index", posts);
  11. }
  12. }

Define View

Add an index view and make it inherit from Viewpage<list<post>>.

1. Use the inline code display to cycle through the data and use the HtmlHelper method provided by ViewPage.

[HTML]View Plaincopy
  1. <h3>1. Using inline code </h3>
  2. <%=html.actionlink ("Home", new { action="Index"})%> |
  3. <div>
  4. <%foreach (post post in ViewData)
  5. {%>
  6. <div class="PostItem">
  7. <strong>title</strong;:<%=html.encode (post. Title)%></br>
  8. <strong>author</strong;:<%=html.encode (post. Author)%></br>
  9. <strong>pubdate</strong;:<%=html.encode (post. Pubdate.toshortdatestring ())%></br>
  10. <strong>content</strong;:<%=html.encode (post. Description)%></br>
  11. </div><br />
  12. <%}%>
  13. </div>


2. Using the server control ListView, write the following code:

[HTML]View Plaincopy
  1. <h3> Using the ListView control </h3>
  2. <asp:listview id="ListView1" runat= "server">
  3. <layouttemplate>
  4. <div>
  5. <asp:placeholder id="Itemplaceholder" runat="Server"/>
  6. </div>
  7. </layouttemplate>
  8. <ItemTemplate>
  9. <div class="PostItem">
  10. <strong>title</strong;:<%# Eval ("Title")%></ </br>
  11. <strong>author</strong;:<%# Eval ("Author")%></BR >
  12. <strong>pubdate</strong;:<%# Eval ("PubDate")%> </br>
  13. <strong>content</strong;:<%# Eval ("Description")%></ br>
  14. </div><br />
  15. </ItemTemplate>
  16. </asp:listview>


The data binding of the ListView in the background code is simply to bind the view data to the ListView and get the data from the database to the controller.

[CSharp]View Plaincopy
    1. Public partial class views_blog_index:viewpage<list<post>>
    2. {
    3. protected void Page_Load (object sender, EventArgs e)
    4. {
    5. This .  Listview1.datasource = ViewData;
    6. This .  Listview1.databind ();
    7. }
    8. }

Set PATH selection

Also we need to set the path selection

[CSharp]View Plaincopy
    1. void Application_Start (object sender, EventArgs e)
    2. {
    3. //Code that runs on application startup
    4. ROUTETABLE.ROUTES.ADD (
    5. New Route
    6. {
    7. URL = "[Controller]/[action].mvc",
    8. Defaults = new {action = "Index"},
    9. Routehandler = typeof (Mvcroutehandler)
    10. });
    11. }

Go to ASP. NET MVC display list data

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.