ASP. net mvc 2.0 display list and detailed page operations

Source: Internet
Author: User
Tags actionlink

About ASP. net mvc Framework, BKJIA-.NET channel recommended to you, this series of articles also include

Article 1 Introduction to ASP. net mvc 2.0 MVC Framework

Article 2 ASP. net mvc 2.0 how to run WEB Applications

Article 3 add an ASP. net mvc 2.0

Create View Index and NewsDetails

Create a news homepage to display the news list.

In the Views/News directory, right-click and choose Add-> View to modify the configuration, as shown in

Modify the display items in the generated HTML code. The main code is as follows:

 
 
  1. <% foreach (var item in Model) { %> 
  2.         <tr> 
  3.             <td> 
  4.       <%: Html.ActionLink("Edit", "NewsEdit", new { id=item.Id }) %> |  
  5.     <%: Html.ActionLink("Details", "NewsDetails", new { id=item.Id })%> |  
  6.  <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%> 
  7.             </td> 
  8.             <td> 
  9.                 <%: item.Title %> 
  10.             </td> 
  11.             <td> 
  12.                 <%: String.Format("{0:g}", item.CreateTime) %> 
  13.             </td> 
  14.             <td> 
  15.                 <%: item.Content %> 
  16.             </td> 
  17.         </tr> 
  18.     <% } %>  

Use Foreach to traverse records in the News List cyclically.

 
 
  1. <%: Html.ActionLink("Details", "NewsDetails", new { id=item.Id })%> 

The connection URL searches for the NewsDetails Action method under the current Controller and transmits the value with the news Id as the parameter.

Create NewsDetails. asp

The generated core code is as follows:

 
 
  1. <Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
  2. <H2> details
  3. <Fieldset>
  4. <Legend> News </legend>
  5. <Div class = "display-label"> title </div>
  6. <Div class = "display-field"> <%: Model. Title %> </div>
  7. <Div class = "display-label"> creation time </div>
  8. <Div class = "display-field"> <%: String. Format ("{0: g}", Model. CreateTime) %>
  9. </Div>
  10. <Div class = "display-label"> news content </div>
  11. <Div class = "display-field"> <%: Model. Content %> </div>
  12. </Fieldset>
  13. <P>
  14. <%: Html. ActionLink ("Edit", "NewsEdit", new {id = Model. Id}) %> |
  15. <%: Html. ActionLink ("Back to List", "Index") %>
  16. </P>
  17. </Asp: Content>

<%: Html. actionLink ("Edit", "NewsEdit", new {id = Model. id}) %> | this connection will jump to the news editing page and pass the value with the news Id.

Modify the Controller File

Under the Controllers/News File

Modify Action Name = Index to initialize data on the Index. aspx page. The database is not read here, but some data is forged and put in static variables:

 
 
  1. public static List<THelperMVC.Models.News.NewsModel> newsList; 

The Index Action Code is as follows:

 
 
  1. Public ActionResult Index ()
  2. {
  3. NewsList = new List <THelperMVC. Models. News. NewsModel> ();
  4. For (int I = 0; I <10; I ++)
  5. {
  6. THelperMVC. Models. News. NewsModel news = new THelperMVC. Models. News. NewsModel ();
  7. News. Id = I;
  8. News. Title = "Title" + I. ToString ();
  9. News. CreateTime = System. DateTime. Now;
  10. News. Content = "new Content? Wen? Newar2 rongyu Y "+ I. ToString ();
  11. NewsList. Add (news );
  12. }
  13. Return View (newsList );
  14. }

Use the For loop to generate 10 news records.

Modify the Action method corresponding to NewsDetails. Aspx as follows:

 
 
  1. // GET: /News/Details/5  
  2. public ActionResult NewsDetails(int id)  
  3. {  
  4.      THelperMVC.Models.News.NewsModel news=newsList[id];  
  5.      return View(news);  
  6. }   

Find the NewsModel object from the Global static variable based on the URL parameter, that is, the news Id, to initialize the news details page.

Finally, modify the News connection on the master page, as shown in:

Click the News hyperlink on the homepage to find the Index method in the NewsController folder, and initialize the Views/News/Index. aspx page.

Program Running Effect

Press Ctrl + F5 to run the program, as shown in:

Click "News" to go to the News list page, as shown in:

Click the Details hyperlink to go to the Details page of the corresponding record, as shown in:

Original article title: Asp.net MVC2.0 series of articles-display list and detailed page operations

Link: http://www.cnblogs.com/ywqu/archive/2010/06/28/1766403.html

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.