The Delete action method for implementing HTTP-GET based on MVC Learning

Source: Internet
Author: User
Implement HTTP-GET Delete action Method Now, add the delete function to the dinnerscontroller class. When a user accesses the/dinners/delete/[ID] URL, the delete action method is accessed. The implementation of this method is as follows: // // Http get:/dinners/delete/1 Public ActionresultDelete (IntID) { DinnerDinner = dinnerrepository. getdinner (ID ); If(Dinner =Null) ReturnView ("Notfound"); Else ReturnView (dinner ); } The action method tries to retrieve the dinner object to be deleted. If a dinner object exists, the view of the dinner object is displayed. If the object does not exist (or has been deleted), a notfound view is returned. We have created this view before. The methods for creating the delete view are not repeated, as shown in:
After you click the Add button, Visual Studio automatically creates a new Delete. aspx view template file under the \ views \ dinners directory. We will modify the file to confirm deletion. Code As follows: < ASP : Content ID= "Title" Contentplaceholderid= "Titlecontent" Runat= "Server"> Confirm deletion:<%=Html. encode (model. Title)%> </ ASP : Content > < ASP : Content ID= "Main" Contentplaceholderid= "Maincontent" Runat= "Server"> <H2> Are you sure you want to delete it?</H2> <Div> <P> Please confirm whether to cancel the banquet:<I> <%=Html. encode (model. Title)%></I> </P> </Div> <% Using(Html. beginform ()) {%> <Input Name= "Confirmbutton" Type= "Submit" Value=" Delete" /> <%}%> </ ASP : Content > The code above shows the dinner to be deleted and outputs the <form> element. When you click the delete button, the form is submitted to the/dinners/delete/[ID] address. Run the application Program , Access the/dinners/delete/[ID] URL and ID identifies a valid dinner object. The effect is shown in the browser as follows:

Implement HTTP-POST Delete action Method The code for implementing the http post action of the delete action method is as follows: // // Http post:/dinners/delete/2 [ Acceptverbs(Httpverbs. Post)] Public ActionresultDelete (IntID,StringConfirmbutton) { DinnerDinner = dinnerrepository. getdinner (ID ); If(Dinner =Null) ReturnView ("Notfound"); Dinnerrepository. Delete (dinner ); Dinnerrepository. Save (); ReturnView ("Deleted"); } The HTTP-POST version of the delete action method first retrieves the dinner object to be deleted. If not found (may have been deleted), The notfound view template is displayed. If yes, It is deleted from dinnerrepository and the deleted view template is displayed. Now you need to create a deleted view template. In the add view dialog box, you do not need to select a strong model type, as shown in:
Run the sample program again to access the/dinners/delete/[ID] URL address. In the confirm deletion window, click Delete, perform the HTTP-POST operation to the/dinners/delete/[ID] address, trigger the post version of the delete method in dinnerscontroller, delete the specified dinner record from the database, and display the deleted view template:

Click the "show upcoming banquet list" link and return to the/dinners address. The deleted dinnerd record does not exist.

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.