asp.net MVC Framework Experience (3): Form submission

Source: Internet
Author: User

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>

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.