ASP. net mvc 4 practice 2: The first MVC application (below ),

Source: Internet
Author: User
Tags actionlink

ASP. net mvc 4 practice 2: The first MVC application (below ),

In the previous article, VS helped us create a simple operation page and implement the CRUD function. However, this article tries to analyze how it is implemented.

1. View (take Create view as an example ):

1 @ * Note: in this section, the left and right sides are enclosed in the comment * @ 2 3 @ *. The @ model below indicates that data or objects are transmitted to the view template using a strongly typed Method * @ 4 5 @ * @ prefix indicates conversion between HTML and code * @ 6 7 @ model Guestbook. models. guestbookEntry 8 9 @ {10 ViewBag. title = "Create"; 11} 12 13 2. Controller:

1 namespace Guestbook. controllers 2 {3 public class GuestbookController: Controller 4 {5 private GuestbookContext db = new GuestbookContext (); 6 7 // GET:/Guestbook/8 public ActionResult Index () 9 {10 return View (db. entries. toList (); 11} 12 13 // GET:/Guestbook/Details/5 14 public ActionResult Details (int? Id) 15 {16 if (id = null) 17 {18 return new HttpStatusCodeResult (HttpStatusCode. badRequest); 19} 20 GuestbookEntry guestbookentry = db. entries. find (id); 21 if (guestbookentry = null) 22 {23 return HttpNotFound (); 24} 25 return View (guestbookentry); 26} 27 28 // GET: /Guestbook/Create 29 public ActionResult Create () 30 {31 return View (); 32} 33 34 // POST:/Guestbook/Create 35 // To protect from overposting attacks, please enable the specific properties you want to bind to, for 36 // more details see http://go.microsoft.com/fwlink/?LinkId=317598 . 37 [HttpPost] 38 [ValidateAntiForgeryToken] 39 public ActionResult Create ([Bind (Include = "Id, Name, Message, DateAdded")] GuestbookEntry guestbookentry) 40 {41 if (ModelState. isValid) 42 {43 db. entries. add (guestbookentry); 44 db. saveChanges (); 45 return RedirectToAction ("Index"); 46} 47 48 return View (guestbookentry); 49} 50 51 // GET: /Guestbook/Edit/5 52 public ActionResult Edit (int? Id) 53 {54 if (id = null) 55 {56 return new HttpStatusCodeResult (HttpStatusCode. badRequest); 57} 58 GuestbookEntry guestbookentry = db. entries. find (id); 59 if (guestbookentry = null) 60 {61 return HttpNotFound (); 62} 63 return View (guestbookentry); 64} 65 66 // POST: /Guestbook/Edit/5 67 // To protect from overposting attacks, please enable the specific properties you want to bind to, for 68 // more details see http://go.microsoft.com/fwlink/?LinkId=317598 . 69 [HttpPost] 70 [ValidateAntiForgeryToken] 71 public ActionResult Edit ([Bind (Include = "Id, Name, Message, DateAdded")] GuestbookEntry guestbookentry) 72 {73 if (ModelState. isValid) 74 {75 db. entry (guestbookentry ). state = EntityState. modified; 76 db. saveChanges (); 77 return RedirectToAction ("Index"); 78} 79 return View (guestbookentry); 80} 81 82 // GET:/Guestbook/Delete/5 83 public ActionResult Delete (int? Id) 84 {85 if (id = null) 86 {87 return new HttpStatusCodeResult (HttpStatusCode. badRequest); 88} 89 GuestbookEntry guestbookentry = db. entries. find (id); 90 if (guestbookentry = null) 91 {92 return HttpNotFound (); 93} 94 return View (guestbookentry); 95} 96 97 // POST: /Guestbook/Delete/5 98 [HttpPost, ActionName ("Delete")] 99 [ValidateAntiForgeryToken] 100 public ActionResult DeleteConfirmed (int id) 101 {102 GuestbookEntry guestbookentry = db. entries. find (id); 103 db. entries. remove (guestbookentry); 104 db. saveChanges (); 105 return RedirectToAction ("Index"); 106} 107 108 protected override void Dispose (bool disposing) 109 {110 if (disposing) 111 {112 db. dispose (); 113} 114 base. dispose (disposing); 115} 116} 117}View Code

From the code above, we can see that the Create and Edit methods have two overloading methods.[In fact, the Delete method also has two reloads, but because these two methods have the same type and number of parameters, the other is named DeleteConfirmed, and then add ActionName ("Delete ") annotation indicates the action to process the Delete View]. Two methods with the same name are rendered at loading, and the other is used to process the action of the view (modified using the HttpPost annotation attribute ).

3. Layout:

Do you feel that our CRUD Page uses something like Master Page? Yes, the default Layout is Views \ Shared \ _ Layout. cshtml. The Code is as follows:

1 <! DOCTYPE html> 2 How is the view associated with the default layout? The answer is in Views \ _ ViewStart. cshtml:

1 @ {2 Layout = "~ /Views/Shared/_ Layout. cshtml "; 3}View Code

_ ViewStart is executed before all views are executed. It sets the default Layout. If Layout is not specified for a view, the default Layout is used.




Related Article

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.