ASP.NET MVC Framework體驗(3):表單提交

來源:互聯網
上載者:User

概述

在前面的兩篇文章總,我們分別做了一個簡單的ASP.NET MVC的例子和進行資料 的綁定,在本文中,將通過ASP.NET MVC Framework實現表單的提交,你可以看到,在這裡 有多種方法來擷取表單資料,可以自動對應、通過Request對象擷取等。

實現新增數 據

1.這裡我們還採用上一篇做過的Blog樣本(在後面的文章中,我將一直使用該樣本) ,在這之前,先修改一下上次樣本中的BlogRepository,為其增加一個Add方法:

public void Add(Post post)
{
  BlogDataContext db = new BlogDataContext();
  db.Posts.InsertOnSubmit(post);
   db.SubmitChanges();
}

2.在Index視圖中添加一個可以轉向建立Post頁面 的連結,使用ActionLink()方法:

<h2>ASP.NET MVC Framework Sample</h2>
<hr />
<%=Html.ActionLink("Home", new { action="Index"})%> |
<%=Html.ActionLink("New Post", new { action="New"})%>
<div>
  <% foreach (Post post in ViewData)
   { %>
  <div class="postitem">
    <strong>Title</strong>: <%=Html.Encode(post.Title) %></br>
     <strong>Author</strong>:<%=Html.Encode(post.Author) % ></br>
    <strong>PubDate</strong>:<% =Html.Encode(post.PubDate.ToShortDateString()) %></br>
     <strong>Content</strong>:<%=Html.Encode(post.Description) % ></br>
  </div><br />
  <% } %>
</div>

在上面的代碼中,第四行我們添加了New Post超連結,並指定該 連結的action為New,這裡我們也可以通過action名稱來指定:

<h2>ASP.NET MVC Framework Sample</h2>
<hr />
<%=Html.ActionLink("Home", "Index")%> |
<%=Html.ActionLink("New Post", "New")%>
<div>
  <%foreach (Post post in ViewData)
   { % >
  <div class="postitem">
     <strong>Title</strong>:<%=Html.Encode(post.Title) % ></br>
    <strong>Author</strong>:<% =Html.Encode(post.Author) %></br>
     <strong>PubDate</strong>:<%=Html.Encode (post.PubDate.ToShortDateString()) %></br>
     <strong>Content</strong>:<%=Html.Encode(post.Description) % ></br>
  </div><br />
  <% } %>
</div>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.