概述
在前面的两篇文章总,我们分别做了一个简单的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>