In this tutorial, you will view the automatically generatedDetailsAndDeleteMethod.
Query details and delete records Open the movie controller and view the details method.
Public actionresult details (INT id = 0) {movie = dB. movies. find (ID); If (movie = NULL) {return httpnotfound ();} return view (movie );}
Code first makes it easy to useFindMethod To search for data. An important security feature is built into the method. Method verification firstFindMethod has found a movie, and then execute otherCode. For example, hackers can modifyHttp: // localhost: xxxx/movies/details/1ToHttp: // localhost: xxxx/movies/details/12345(Or some other values, do not represent the actual value of the video) so that the link URL is incorrect. If you do not check whether movie is found, null movie may cause a data error.
ViewDeleteAndDeleteconfirmedMethod.
// get: /movies/delete/5 Public actionresult Delete (INT id = 0) {movie = dB. movies. find (ID); If (movie = NULL) {return httpnotfound () ;}return view (movie) ;}/// post: /movies/delete/5 [httppost, actionname ("delete")] public actionresult deleteconfirmed (INT id = 0) {movie = dB. movies. find (ID); If (movie = NULL) {return httpnotfound ();} dB. movies. remove (movie); dB. savechanges (); Return redirecttoaction ("Index") ;}
Please note that,DeleteOfHTTP GETThe method does not delete the specified movie. It returns the view for deleting the movie. You can submit (Httppost) Delete a movie. If you use a GET request to perform a delete operation (or perform an edit operation, create operation, or modify any other operations on the data), a security vulnerability is opened. For more information, see Stephen Walther's blog.ASP. net mvc Tip #46-don't use Delete links because they create security holes.
TheHttppostMethod named as a unique signature or nameDeleteconfirmedMethod. The signatures of these two methods are as follows:
// Get:/movies/delete/5 Public actionresult Delete (INT id = 0) /// post:/movies/delete/5 [httppost, actionname ("delete")] public actionresult deleteconfirmed (INT id = 0)
When the Common Language Runtime (CLR) overload method, the method must have a unique signature (the method name is the same but different parameter lists ). However, here you need two deletion methods-one get method and one post method both have the same signature. (They all need to take an integer as the parameter ).
There are several ways to solve this problem. First, use different method names. This is the method used by the Framework Code in the previous example. However, this brings about a small problem: ASP. NET maps some URLs to the operation method by name. If you rename the method, generally routing cannot find this method. The solution is as shown in the following example:Actionname ("delete ")Add propertyDeleteconfirmedMethod. This will effectively execute the routing system URL ing, such a include/Delete/The URL of the POST request will be foundDeleteconfirmedMethod.
Another common method to avoid having the same name and signature is to artificially change the POST method, including signature without parameters. For example, some developers add parameter typesFormcollection,FormcollectionIs passed to the POST method, and then this parameter is not used at all:
Public actionresult Delete (formcollection fcnotused, int id = 0) {movie = dB. movies. find (ID); If (movie = NULL) {return httpnotfound ();} dB. movies. remove (movie); dB. savechanges (); Return redirecttoaction ("Index ");}
Summary
You now have a complete ASP. net mvc applicationProgramAnd store data in the local database. You can create, read, update, delete, and search movies.
If you want to deploy applications, you 'd better test your applications on your local IIS 7 server first. You can use thisWeb platform InstallerLink to enable the ASP. NET application settings of the IIS server. See the following deployment link:
·Test your ASP. net mvc or webforms application on IIS 7 in 30 seconds
·ASP. NET deployment content map
·Enabling IIS 7.x
·Web application projects deployment
Now we encourage you to start learning intermediate contentCreating an Entity Framework data model for an ASP. net mvc applicationAndMVC Music StoreTutorial, browseASP. NET articles on msdnArticle, Let's take a look at a lot of videos and resources:Http://asp.net/mvcTo learn more about ASP. net mvc!ASP. net mvc forumsForum is a good place to useAsk the question you want to know.
Download the complete document:ASP. NET mvc41_1_timeout
Certificate -----------------------------------------------------------------------------------------------------------------------------------------------
Note:
This series contains nine articles translated from ASP. net mvc4 official tutorial, due to the concise description of this series of articles, the length is moderate, from an example to explain, the full text finally completed a small system for managing movies, very suitable for beginners ASP.. Net mvc4. Nine articles:
1. Introduction to ASP. NET mvc4
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4
· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html
2. Add a controller
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller
· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html
3. Add a view
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view
· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html
4. Add a model
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model
· Address: http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html
5. Access the data model from the Controller
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller
· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html
6. Verify the editing method and view
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view
· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html
7. Add new fields to the movie table and Model
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table
· Translation address:Http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html
8. Add a validator to the Data Model
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model
· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/05/2944030.html
9. query details and delete records
· Original address: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods
· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/03/07/2948000.html
10. Third-party control studio for ASP. NET wijmo mvc4 tool Application
Address: http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html