Reference: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods
Let's take a look at the automatically generated Details and Delete methods.
MoviesControllerInDetailsMethod:
public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); }
, Details, Value.
Code First uses the Find Method to make search easier. The Detail method contains an important security feature: the code will not be processed before a specific movie instance is found.
Change
The Delete and DeleteConfirme methods in MoviesController:
// // GET: /Movies/Delete/5 public ActionResult Delete(int id = 0) { Movie 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) { Movie movie = db.Movies.Find(id); db.Movies.Remove(movie); db.SaveChanges(); return RedirectToAction("Index"); }
As we have mentioned before, if no prefix is added before the ActionResult Method, the default Method is the GET request execution Method. This Method does not update the database data, but returns the queried data. When we click "Delete" to connect (http: // localhost: 9898/movies/Delete/1), the Delete method is called to query the data of the corresponding Id, then feedback to the page. Click the Delete button again to submit an http post request and call the ActionDelete method.
Let's take a look at the differences between the Delete method and the DeleteConfirm method:
When a CLR (common language runtime) Request loads a method, it calls the corresponding method based on the unique (unique) method name and parameter. However, here we need two Delete methods, one for GET requests and one for POST requests, which have the same parameters.
To distinguish the two Delete methods, one solution is to rename the Delete method to be called in the POST request as DeleteConfirme. in this case, the Delete URL (http: // localhost: 9898/movies/Delete/1) Request can only call the GET Delete Method, the renamed DeleteConfrime Method cannot be called. To solve this problem, we only need to addActionName("Delete")Attribute.
Summary:
We have established a simple crud mvc Application. Next, we should publish this website. Microsoft provides us with free web hosting and can mount 10 websites.
Address: free Windows Azure trial account.