The Edit Action Method of HTTP-POST in MVC Learning

Source: Internet
Author: User
Tags http redirect
Implement HTTP-POST Edit action Method Now we have implemented the edit action method that supports HTTP-GET. When a user requests a/dinners/edit/2 address, an HTML page is received. Click Save to submit the trigger form to the/dinners/edit/2 URL and submit the value in the <input> form through http post. Next, we start to implement the edit Action Method of HTTP post-processing and saving operations. Add an overloaded edit action method to the dinnerscontroller class, and set the acceptverbs attribute to indicate that the method is responsible for handling http post actions. // // Post:/dinners/edit/2 [Acceptverbs(Httpverbs. Post)] Public ActionresultEdit (IntID,FormcollectionFormvalues ){ } After the [acceptverbs] attribute is added to the overloaded action method, ASP. net mvc automatically distributes the request to the appropriate action Method Based on the HTTP action. For http post requests/dinners/edit/[ID], the preceding edit method is used for processing, however, all other HTTP requests/dinners/edit/[ID] will have the previously defined Edit Method (this method does not have the [acceptverbs] attribute ). Obtain the value of Form submission. There are many methods in the Edit Method of http post to obtain the form parameter value. A simple method is to use the request attribute of the controller base class to access the form set and directly obtain the submitted parameter values: // // Post:/dinners/edit/2 [Acceptverbs(Httpverbs. Post)] Public ActionresultEdit (IntID,FormcollectionFormvalues) { // Retrieve existing dinner DinnerDinner = dinnerrepository. getdinner (ID ); // Update dinner with form posted values Dinner. Title = request. Form ["Title"]; Dinner. Description = request. Form ["Description"]; Dinner. eventdate =Datetime. Parse (request. Form ["Eventdate"]); Dinner. Address = request. Form ["Address"]; Dinner. Country = request. Form ["Country"]; Dinner. contactphone = request. Form ["Contactphone"]; // Persist changes back to database Dinnerrepository. Save (); // Perform HTTP redirect to details page for the saved dinner ReturnRedirecttoaction ("Details",New{Id = dinner. dinnerid }); } The above method is a bit cumbersome, especially after the exception handling logic is added. A better way is to use the built-in method updatemodel () of the controller base class (). This method supports updating the attributes of an object using input form parameters. It uses the reflection mechanism to parse the attribute names of an object, and then automatically assigns values to relevant attributes based on the parameter values passed by the client. Next, we use the updatemodel () method to implement the previous HTTP-POST edit action method: // // Post:/dinners/edit/2 [Acceptverbs(Httpverbs. Post)] Public ActionresultEdit (IntID,FormcollectionFormvalues) { // Retrieve existing dinner DinnerDinner = dinnerrepository. getdinner (ID ); Updatemodel (dinner ); // Persist changes back to database Dinnerrepository. Save (); // Perform HTTP redirect to details page for the saved dinner ReturnRedirecttoaction ("Details",New{Id = dinner. dinnerid }); } Method/dinners/edit/2 again, and change the title and Event Date of dinner:

Click the Save button, execute Form submission, trigger the Edit Method call, and persistently update the value to the database. Next, redirect to the details page-Details View (display the latest data ).
An error occurred while handling the editing. The current HTTP-POST implementation method works fine-of course there will be exceptions. When a user makes a mistake when editing a form, we need to ensure that the form displays an error message and instruct the user to correct it. This includes the incorrect data submitted by the user (such as the incorrect date format), or the conflict of business rules. When an error occurs, the form must keep the initial data that the user enters so that they do not have to repeat the data. This process needs to be repeated multiple times until the form is successfully submitted. ASP. net mvc includes some friendly built-in functions to make it easier to handle exceptions and display forms again. To demonstrate these functions, we will update the edit action method again, Code As follows: // // Post:/dinners/edit/2 [Acceptverbs(Httpverbs. Post)] Public ActionresultEdit (IntID,FormcollectionFormvalues) { DinnerDinner = dinnerrepository. getdinner (ID ); Try { Updatemodel (dinner ); Dinnerrepository. Save (); ReturnRedirecttoaction ("Details",New{Id = dinner. dinnerid }); } Catch { Foreach(VaRIssueInDinner. getruleviolations ()) { Modelstate. addmodelerror (issue. propertyname, issue. errormessage ); } ReturnView (dinner ); } } The above code is basically the same as the previous implementation, except for adding a try/catch exception capture code block. If an exception occurs when calling the updatemodel () method or when saving dinnerrepository (if we try to save an invalid object-Rule conflict, an exception will be thrown), the exception capture code block will be triggered for execution. In the catch code block, first traverse all rule conflicts in the dinner object, add them to the modelstate object, and then re-display the view. The following code re-runs the application to simulate exception information Program Edit the dinner, clear the title, set the Event Date eventdate to entlib, and call format. Then click Save. Now the Edit Method is triggered by http post, however, you cannot successfully Save the dinner information (because of an exception) and re-display the form:
Now, applications have a better exception handling mechanism. The text box with invalid input is displayed in red * and the error message is displayed on the interface. The form also retains the information that the user originally entered-so that they do not have to repeat the entry. How are these implemented? This is because we use some built-in ASP. net mvc functions to make input verification and Exception Handling easier.

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.