Overview
In the previous two articles, we made a simple example of asp.net mvc and binding data, and in this article, we will implement the form submission through the ASP.net MVC framework, and you can see that there are a number of ways to get form data, which can be automatically mapped, Get through the request object and so on.
Implementing new Data
1. Here we also use the previous blog example (which I will always use in the following article) to modify the blogrepository in the last example to add an Add method:
public void Add(Post post)
{
BlogDataContext db = new BlogDataContext();
db.Posts.InsertOnSubmit(post);
db.SubmitChanges();
}
2. Add a link to the new post page in the index view, using the ActionLink () method:
<%=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>
In the above code, we add the new post hyperlink in line fourth and specify that the link action is new, which we can also specify by the action name:
<%=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>