Overview
In the previous two articles, we made a simple ASP. net mvc example and data binding, in this article, through ASP. net mvc Framework implements form submission. You can see that there are multiple methods to obtain form data, which can be automatically mapped and obtained through the Request object.
Implement new data
1. here we also use the Blog example we have done in the previous article (I will always use this example in later articles). Before that, modify the BlogRepository in the previous example, add an Add method for it:
public void Add(Post post){ BlogDataContext db = new BlogDataContext(); db.Posts.InsertOnSubmit(post); db.SubmitChanges();}
2. Add a link in the Index view that can be switched to the new Post page. Use 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 code above, the New Post hyperlink is added in the fourth line, and the action of the link is specified as New. Here we can also specify 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>
3. Write the New action code in the controller. The code here is very simple, because we only need to switch to the New Post view, and no other operations are required:
[ControllerAction] public void New () {// turn to the New page RenderView ("New ");}
4. Compile the New view. Here we provide some forms for users to input data. The HTML code is as follows:
Next, Add an HTML form element and specify its action as the action for adding a new Post. Here we assume it is Add and the method is Post, the final code should look as follows:
Get form data
After completing the above steps, you can write the Add action code in the Controller. here you need to obtain the form data and write it to the database, ASP. net mvc Framework provides multiple methods to obtain form data.
1. Automatic ing
To use this method, we only need to define the form name, ASP. net mvc Framework will automatically help us map and directly assign the form data to the method parameter corresponding to the form Action, as shown in the following code:
[ControllerAction]public void Add(string title, string author, string description){ Post post = new Post(); post.Title = title; post.Author = author; post.Description = description; post.PubDate = DateTime.Now; BlogRepository repository = new BlogRepository(); repository.Add(post); RedirectToAction("Index");}
2. when using the first method, if there are too many forms on the interface, it will increase the parameters of the method, which seems to be the "dirty code" mentioned in the refactoring. To solve this problem, we can also use form variables to obtain the value of the form, so that the code looks as follows:
[ControllerAction]public void Add(){ Post post = new Post(); post.Title = Request.Form["title"]; post.Author = Request.Form["author"]; post.Description = Request.Form["description"]; post.PubDate = DateTime.Now; BlogRepository repository = new BlogRepository(); repository.Add(post); RedirectToAction("Index");}
The above two methods are to get the value of the form, save it to the database, and turn to the Index action for processing.
3. MVCToolkit also provides some extension methods that can be used to obtain the value of a form.
Select Configuration path
This step cannot be forgotten at any time. This time we modify it into the form of controller/action. The Code is as follows:
void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RouteTable.Routes.Add( new Route { Url = "[controller]/[action]", Defaults = new { action = "Index" }, RouteHandler = typeof(MvcRouteHandler) });}Complete data increase
The preceding example is as follows. First, there is a New Post link in the Index View:
Click New Post:
After submission, add a new Post and switch to the Index View:
Conclusion
For more information about ASP. the simple example of Form submission in the net mvc Framework ends here. I believe that through the previous three simple articles, we will discuss ASP. net mvc Framework has a rough understanding. I will write some in-depth articles and some HTML Helper methods provided by MVCToolkti later :)
Download the sample code:/Files/Terrylee/MVCDemo03.rar