Asp. Net MVC series: access Model data from Controller, mvcmodel

Source: Internet
Author: User

Asp. Net MVC series: access Model data from Controller, mvcmodel

In the project solution, add a MoviesController controller, select the corresponding template, and model class and data context. For how to add model classes and data context, go to ASP. net mvc series: added Models

After you click "OK", you will find that Visual Studio has created a method for adding, deleting, modifying, and querying data for you, and a view is generated.

After running the program, you can directly open the http: // localhost: 60534/Movies homepage to view the functions generated by visual studio.

In the generated controller code, you will find that the Controller will create an instance of MovieDBContext (Database context), and then all data operations will call the MovieDBContext instance method.

1 using System; 2 using System. collections. generic; 3 using System. data; 4 using System. data. entity; 5 using System. linq; 6 using System. web; 7 using System. web. mvc; 8 using MvcMovie. models; 9 10 namespace MvcMovie. controllers 11 {12 public class MoviesController: Controller 13 {14 private MovieDBContext db = new MovieDBContext (); 15 16 // 17 // GET:/Movies/18 19 public ActionResult Index () 20 {21 return View (db. movies. toList (); 22} 23 24 // 25 // GET:/Movies/Details/5 26 27 public ActionResult Details (int id = 0) 28 {29 Movie movie = db. movies. find (id); 30 if (movie = null) 31 {32 return HttpNotFound (); 33} 34 return View (movie ); 35} 36 37 // 38 // GET:/Movies/Create 39 40 public ActionResult Create () 41 {42 return View (); 43} 44 45 // 46 // POST:/Movies/Create 47 48 [HttpPost] 49 [ValidateAntiForgeryToken] 50 public ActionResult Create (Movie movie) 51 {52 if (ModelState. isValid) 53 {54 db. movies. add (movie); 55 db. saveChanges (); 56 return RedirectToAction ("Index"); 57} 58 59 return View (movie); 60} 61 62 // 63 // GET: /Movies/Edit/5 64 65 public ActionResult Edit (int id = 0) 66 {67 Movie movie = db. movies. find (id); 68 if (movie = null) 69 {70 return HttpNotFound (); 71} 72 return View (movie ); 73} 74 75 // 76 // POST:/Movies/Edit/5 77 78 [HttpPost] 79 [ValidateAntiForgeryToken] 80 public ActionResult Edit (Movie movie) 81 {82 if (ModelState. isValid) 83 {84 db. entry (movie ). state = EntityState. modified; 85 db. saveChanges (); 86 return RedirectToAction ("Index"); 87} 88 return View (movie); 89} 90 91 // 92 // GET: /Movies/Delete/5 93 94 public ActionResult Delete (int id = 0) 95 {96 Movie movie = db. movies. find (id); 97 if (movie = null) 98 {99 return HttpNotFound (); 100} 101 return View (movie ); 102} 103 104 // 105 // POST:/Movies/Delete/5106 107 [HttpPost, ActionName ("Delete")] 108 [ValidateAntiForgeryToken] 109 public ActionResult DeleteConfirmed (int id) 110 {111 Movie movie = db. movies. find (id); 112 db. movies. remove (movie); 113 db. saveChanges (); 114 return RedirectToAction ("Index"); 115} 116 117 protected override void Dispose (bool disposing) 118 {119 db. depose (); 120 base. dispose (disposing); 121} 122} 123}MoviesController

At the top of the view page, you will find such a line of code. This line of code is used to specify the object model of the view.

@model MvcMovie.Models.Movie 

And allows you to access and set the data members of the class object instance in the view.

 

Finally, we found that visual studio also helped us generate the local database file and the table structure of the database.

  http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller 

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.