Address: http://weblogs.asp.net/stephenwalther/archive/2008/06/20/asp-net-mvc-tip-6-call-redirecttoaction-after-submitting-a-form.aspx
Abstract: In this Tip, Stephen Walther describes why the RedirectToAction method needs to be called to redirect a submitted form, rather than directly returning a view.
Suppose you are using an HTML form to collect information from the user. The HTML form is named HomeController. the Create () controller action is displayed, and the data is submitted to the Controller named HomeController. new () controller action, which adds the data in the form to the database. After the form data is submitted, you want to display the aggregate statistical result (1 ).
Figure 1-Results. aspx View
There are two methods to compile the New () controller action. In listing 1, New () action first submits the form data to the database (using LINQ to SQL), and then calls RedirectToAction () to redirect the user to Results () action. In Listing 2, New () action does not call RedirectToAction (). Instead, the Results. aspx view is returned directly from New () action.
Listing 1-SurveyController. vb
1 Public Class SurveyControllerClass SurveyController
2 Inherits System. Web. Mvc. Controller
3
4 Private _ db As New SurveyDataContext ()
5
6 Function Create ()
7 Return View ()
8 End Function
9
10 Function [() Function [New] (ByVal favoriteColor As String)
11
12 'add new survey results to database
13 Dim newSurvey As New Survey ()
14 newSurvey. FavoriteColor = favoriteColor
15 newSurvey. EntryDate = DateTime. Now
16 _ db. Surveys. InsertOnSubmit (newSurvey)
17 _ db. SubmitChanges ()
18
19 'redirect to Confirm action
20 Return RedirectToAction ("Results ")
21 End Function
22
23 Function Results ()
24 Return View (_ db. Surveys)
25 End Function
26
27End Class
Listing 2-Survey2Controller. vb
1 Public Class Survey2ControllerClass Survey2Controller
2 Inherits System. Web. Mvc. Controller
3
4 Private _ db As New SurveyDataContext ()
5
6 Function Create ()
7 Return View ()
8 End Function
9
10 Function [() Function [New] (ByVal favoriteColor As String)
11
12 'add new survey results to database
13 Dim newSurvey As New Survey ()
14 newSurvey. FavoriteColor = favoriteColor
15 newSurvey. EntryDate = DateTime. Now
16 _ db. Surveys. InsertOnSubmit (newSurvey)
17 _ db. SubmitChanges ()
18
19 'Return Results view
20 Return View ("Results", _ db. Surveys)
21 End Function
22
23End Class
Therefore, there are two ways to display the result page after submitting the form data. You can return either RedirectToAction () or View (). Which one is better?
When you call RedirectToAction (), the ASP. net mvc Framework will generate a new redirection for the Web browser. The RedirectToAction () method returns a 302-Object Moved status to the browser. The browser then obtains the Results view.
Therefore, you may think that calling RedirectToAction () is a bad method. When calling RedirectToAction (), the browser will do more work. A network error may occur when the browser initiates a new request. The practical RedirectToAction () increases the possibility of errors.
However, there are three good reasons: RedirectToAction () is better than directly returning a view. There are two reasons: actual and philosophical. Let's start with the actual reasons. If no redirection is performed, and the user clicks the refresh/reload button of the browser, the database data will be submitted more than once. In other words, if no redirection is performed, duplicate data may appear in the database table.
Nowadays, many popular browsers warn users of this risk. Microsoft Internet Explorer 7.0 provides 2 warnings. Therefore, this dangerous situation may not be the worst in the past.
Figure 2-warning of refreshing IE after form submission
The second reason is related to the first one. If you place the result page in a bookmarks/favorites folder (or send the result page link to a friend via Email), and then use the bookmarks to open the page, data operations will occur without prompt. This will submit a form without data, and the client verification will be ignored. You will get 3 ugly pages:
Figure 3-use bookmarks to return to the result page
The third reason why RedirectToAction () is superior to View () is philosophical. The ASP. net mvc Framework provides a "static" interface for your applications. Different URLs indicate different operations. If you return the Results view in the New () action, the corresponding relationship between the action and the view will be damaged. In other words, in a "static" application, the view you see should correspond to the URL address you see in the browser address bar.
If you call RedirectToAction () after submitting the form data, you will see an address like Survey/Results in the browser's address bar. If you call View () after the form is submitted, Survey/New is the address you see in the browser address bar. Because you see the Results page, the first scenario is more meaningful. The address bar of the browser should reflect the status of the Web application. Redirection can ensure synchronization between the browser and the server.
* ******* (From the original author) *********
The best thing to work with Microsoft is that you are always surrounded by a group of very smart people. After posting this Blog, I ran into the room of Brad Wilson (known as xUnit) and told me that a mode was applied to the topic of this Tip. This mode is called the PRG mode (Post-Redirect-Get mode ). Here is the entry to this mode in Wikipedia:
Http://en.wikipedia.org/wiki/Post/Redirect/Get
Therefore, this Tip should be renamed "using the PRG mode when submitting the form ".
Download the source code here: http://weblogs.asp.net/blogs/stephen enwalther/downloads/tip6/tip6.zip.
-----
Advertisement: [. NET regular expression library] http://regex-lib.net /.
-----
Statement:
The original author has already written to Tip #18. Regarding the slow progress, Liu will say sorry here! In addition, you are welcome to read the original article directly. Finally, please log on.