ASP. MVC4 Getting Started Guide (9): query details and delete records

Source: Internet
Author: User

In this tutorial, you'll look at the details and delete methods that are automatically generated.

Query details and delete records

Open the movie controller and view the details method.

 Public ActionResult Details (int0= db. Movies.find (ID); if NULL ) {return  httpnotfound ();} return View (movie);}

Code first allows you to easily use the Find method to search for data. An important security feature is built into the method. Method first verifies that the Find method has found a movie and then executes other code. For example, hackers can change HTTP://LOCALHOST:XXXX/MOVIES/DETAILS/1 to http://localhost:xxxx/Movies/Details/12345 (or some other value, Does not represent the value of the actual movie), which causes the link URL to appear incorrectly. If you do not detect whether the movie was found, null movie causes a data error.

View the Delete and Deleteconfirmed methods.

//GET:/MOVIES/DELETE/5 PublicActionResult Delete (intID =0) {movie Movie=db. Movies.find (ID);if(Movie = =NULL){returnhttpnotfound ();}returnView (movie);}////POST:/MOVIES/DELETE/5[HttpPost, ActionName ("Delete")] PublicActionResult deleteconfirmed (intID =0) {movie Movie=db. Movies.find (ID);if(Movie = =NULL){returnhttpnotfound ();} Db. Movies.remove (movie);d B. SaveChanges ();returnRedirecttoaction ("Index");}

Note that the HTTP Get method of Delete does not delete the specified movie, it returns the view where the movie was deleted, and you can submit (HttpPost) Delete the movie in this view. If you use a GET request to perform a delete operation (or to perform an edit operation, create an action, or any other action that changes the data), a security vulnerability is created. For more information on this, see Stephen • Walter's Blog ASP. NET MVC Tip #46-don ' t use Delete Links because they create Security Holes.

The Deleteconfirmed method that names the HttpPost method of the deleted data as a unique signature or name. The signatures of these two methods are as follows:

// GET:/MOVIES/DELETE/5  Public ActionResult Delete (int0)// /// POST:/MOVIES/DELETE/5[ HttpPost, ActionName ("Delete")] public ActionResult deleteconfirmed (int0)

When the common language runtime (CLR) Overloads a method, the method requires a unique signature (the same method name but a different parameter list). However, here you need two ways to delete-a GET method and a post method that both have the same signature. (They all need to accept an integer as an argument).

There are several ways to address this. One is to use a different method name. This is the method used by the framework code in the previous example. However, this poses a small problem: ASP. NET will map part of the URL by name to the action method, and if you rename the method, usually routing will not find the method. The workaround is what you see in the example, adding the ActionName ("Delete") property to the Deleteconfirmed method. This effectively executes the URL mapping of the routing system, so that a URL containing the/delete/POST request will find the Deleteconfirmed method.

Another common way to avoid methods that have the same name and signature is to artificially change the post method, including signatures that do not use parameters. For example, some developers who add parameter types Formcollection,formcollection are passed to the POST method and do not use this parameter at all:

 Public int 0  = db. Movies.find (ID); if NULL ) {return  httpnotfound ();} Db. Movies.remove (movie);d B. SaveChanges (); return Redirecttoaction ("Index");}

Summarize

You now have a complete ASP. NET MVC application and store the data in the local DB database. You can create, read, update, delete, and search movies.

If you want to deploy your application, it's a good idea to test your application on your local IIS 7 server. You can use this Web Platform Installer link to enable settings for the IIS server's ASP. See the following deployment links:

· Test your ASP. NET MVC or WebForms application on IIS 7 in seconds

· ASP. Deployment Content Map

· Enabling IIS 7.x

· WEB Application Projects Deployment

You are now encouraged to start learning intermediate content Creating an Entity Framework Data Model for an ASP application and MVC Music Store Tutorial, browse ASP. NET Arti Cles on MSDN, the article, and then look at a lot of videos and resources: Http://asp.net/mvc to learn more about ASP. The ASP. NET MVC Forums Forum is a great place to ask questions you want to know.

Full document Download: ASP. MVC4 Getting Started Guide. pdf

--------------------------------------------------------------------------------------------------------------- --------------------------------

Translator Note:

This series of 9 articles, translated from the official ASP. NET MVC4 tutorial, because this series of articles concise, space moderate, from an example to explain, the full text finally completed a small system to manage the film, very suitable for the novice MVC4 ASP, and start the development work. 9 Articles for:

1. Introduction to ASP. 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. Accessing 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. Validating editing methods and editing views

· 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 a new field 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

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. Adding 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. Wijmo MVC4 Tools App

· Address: http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html

ASP. MVC4 Getting Started Guide (9): query details and delete records

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.