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:
- <% foreach (var item in Model) { %>
- <tr>
- <td>
- <%: Html.ActionLink("Edit", "NewsEdit", new { id=item.Id }) %> |
- <%: Html.ActionLink("Details", "NewsDetails", new { id=item.Id })%> |
- <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%>
- </td>
- <td>
- <%: item.Title %>
- </td>
- <td>
- <%: String.Format("{0:g}", item.CreateTime) %>
- </td>
- <td>
- <%: item.Content %>
- </td>
- </tr>
- <% } %>
Use Foreach to traverse records in the News List cyclically.
- <%: 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:
- <Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
- <H2> details
- <Fieldset>
- <Legend> News </legend>
- <Div class = "display-label"> title </div>
- <Div class = "display-field"> <%: Model. Title %> </div>
- <Div class = "display-label"> creation time </div>
- <Div class = "display-field"> <%: String. Format ("{0: g}", Model. CreateTime) %>
- </Div>
- <Div class = "display-label"> news content </div>
- <Div class = "display-field"> <%: Model. Content %> </div>
- </Fieldset>
- <P>
- <%: Html. ActionLink ("Edit", "NewsEdit", new {id = Model. Id}) %> |
- <%: Html. ActionLink ("Back to List", "Index") %>
- </P>
- </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:
- public static List<THelperMVC.Models.News.NewsModel> newsList;
The Index Action Code is as follows:
- Public ActionResult Index ()
- {
- NewsList = new List <THelperMVC. Models. News. NewsModel> ();
- For (int I = 0; I <10; I ++)
- {
- THelperMVC. Models. News. NewsModel news = new THelperMVC. Models. News. NewsModel ();
- News. Id = I;
- News. Title = "Title" + I. ToString ();
- News. CreateTime = System. DateTime. Now;
- News. Content = "new Content? Wen? Newar2 rongyu Y "+ I. ToString ();
- NewsList. Add (news );
- }
- Return View (newsList );
- }
Use the For loop to generate 10 news records.
Modify the Action method corresponding to NewsDetails. Aspx as follows:
- // GET: /News/Details/5
- public ActionResult NewsDetails(int id)
- {
- THelperMVC.Models.News.NewsModel news=newsList[id];
- return View(news);
- }
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