Learn about ASP. NET MVC5 Official Tutorials Summary (11) Details and Delete methods

Source: Internet
Author: User

Learn about ASP. NET MVC5 Official Tutorials Summary (11) Details and Delete methods

In this chapter, we'll look at the automatically generated Details and Delete methods.

Open the Movie controller and find the Details method.

GET:/movies/details/5public actionresult Details (Int32 id) {    movie movie = db. Movies.find (ID);    if (movie = = null)    {        return httpnotfound ();    }    return View (movie);}

Code first finds the Movie object to display by using the Find method . If the movie object is null, then httpnotfound ()is returned, which isa very important judgment.

Delete and deleteconfirm methods

GET:/movies/delete/5public actionresult Delete (Int32 id) {    movie movie = db. Movies.find (ID);    if (movie = = null)    {        return httpnotfound ();    }    return View (movie);} POST:/movies/delete/5[httppost, ActionName ("Delete")][validateantiforgerytoken]public ActionResult Deleteconfirmed (Int32 id) {    movie movie = db. Movies.find (ID);    Db. Movies.remove (movie);    Db. SaveChanges ();    Return redirecttoaction ("Index");}

The first Delete method does not delete the Movie, but instead returns the can delete confirmation page, in the confirmation page, creates the httppost request, completes the deletion of Movie. Deleting a Movie directly with a httpget request will open a security vulnerability.

The method that really deletes the data is named deleteconfirmed. Here are the definitions of the two methods:

Public ActionResult Delete (Int32 id) [HttpPost, ActionName ("delete")][validateantiforgerytoken]public ActionResult Deleteconfirmed (Int32 ID)

The CLR requires the same method name for the refactoring method, but with different parameters. However, the two delete methods that we use here, they all receive an integer parameter, and if the method name is the same, then it is a syntax error.

To solve this problem, there are several workarounds:


The first approach is to use a different method name. This is also the ASP.NET MVC 5  The method used in the stent. However, there are some minor problems with this approach: ASP.NET  The method name is mapped by the address segment, and if the method is renamed, the route will usually not find the method, the solution is already in the above example, we can be deleteconfirmed method Add attribute actionname ("Delete") url contains DELETE  post request will be assigned to method deleteconfirmed

Another common workaround is to use the same name and then Add an unused parameter to the POST method. For example, some developers will add parameters of the formcollection type to the POST method, and then do not use this parameter:

Public ActionResult Delete (formcollection fcnotused, int id = 0) {    movie movie = db. Movies.find (ID);    if (movie = = null)    {        return httpnotfound ();    }    Db. Movies.remove (movie);    Db. SaveChanges ();    Return redirecttoaction ("Index");}
I've written all of my ASP. NET MVC5 Official tutorials here. With my original foundation most of the problems can be solved, hope can also help you.




Learn about ASP. NET MVC5 Official Tutorials Summary (11) Details and Delete methods

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.