ASP. net mvc learning 3. The Controller obtains data from the Model on the left hand and transmits the data to the View page on the right hand.

Source: Internet
Author: User
Tags actionlink

The content of this article: 1. Learn about the specific role of Model 2. Create a Controller to read data from the Model and pass it to the View 3. Learn about Entity Framework Code First 4, clear the forced conversions declared by @ model on the View page. 5. View the View. cshtml's new HTML Writing Method 1: Add a new Model to understand the role of the Model. Model: stores the Model Class, corresponding to the table in the database, access the data in the Table. each instance in the Model Class corresponds to a row in the database Table, and each attribute in the instance corresponds to each column value in the Table (ing between Model Class and DB Table) entity FrameWork relational data model definition. You can see the picture on the right, and you will have time to look back at the connection introduction. http://msdn.microsoft.com/en-us/data/aa937709 Right-click Model floder and Add a new Model Class: Movie. cs 12345678910111213141516171819202122232425 using System; using System. collections. generic; using System. linq; using System. web; using System. data. entity; namespace MVCMovie. models {// Moive class is equivalent to an object instantiated by the Table // Movie class named Movie in the database. It is equivalent to a row in the Table, and each attribute of the Instance (ID, Title ...) equivalent to the public class Movie {public int ID {get; set;} public string Title {g Et; set;} public DateTime ReleaseDate {get; set;} public string Genre {get; set;} public decimal Price {get; set ;}// MovieDBContext class, inherits from DbContext in Entity Framework, representing the Movie data context // MovieDBContext class, reads, stores, and updates the Movie Class instance public class MovieDBContext: System. data. entity. dbContext {public DbSet <Movie> Movies {get; set ;}} configure the WebConfig file: 12345 <connectionStrings> <! -- ModleDBContext connection string to store Movie Data in the database --> <add name = "MovieDBContext" connectionString = "Data Source = (LocalDB) \ v11.0; attachDbFilename = | DataDirectory | \ Movies. mdf; Integrated Security = True "providerName =" System. data. sqlClient "/> <add name =" DefaultConnection "connectionString =" Data Source = (LocalDb) \ v11.0; Initial Catalog = aspnet-MVCMovie-20140304145549; Integrated Security = SSPI; AttachDBFilename = | DataDirecto Ry | \ aspnet-MVCMovie-20140304145549.mdf "providerName =" System. data. sqlClient "/> </connectionStrings> 2. After the solution is rebuilt, Add a new Control to Access Model's Data, learn how the data Controller reads data from the Model Right-click Control floder and add a new control the newly added MovieController automatically has the add, delete, query, and modify function, CRUD (create, read, update, and delete), and the corresponding CRUD View. On the page, you can run creat, list, edit and delete Moive objects (entries). ctrl + F5: http://localhost:9898/movies 2.1 view the code in the automatically generated MoviesController: 1234567891011 // instantiate a MovieDBContext db, and then use the instantiated object to add, query, modify, and delete (GRUD) private MovieDBContext db = new MovieDBContext (); // GET:/Movies/public ActionResult Index () {// return all the Movie instances in the Movie Database return View (db. movies. toList ();} strong model of 2.2, and @ model keyword we have mentioned that ViewBage is a dynamic object, which can pass data or objects from the Controller to the View. ASP. net mvc also provides stronugly typed data to the view template. this type of strong, VS can better check your code and provide intelligent awareness (richer intelliisence) during compilation ). this scaffold mechanism is embodied in the Controller's Methods and View Template's Views. Now let's take a look at the automatically generated code in Controller to copy the code // GET:/Movies/Details /// the following method, read the Movie data from the instantiated Movie object. public ActionResult Details (int id = 0) {// Return to Detail if the Movie corresponding to the id is found. movie movie = db in cshtml. movies. find (id); if (movie = null) {return HttpNotFound ();} return View (movie);} copy the code and go to the Details in the View. cshtml: the first line: 1 @ model MVCMovie. models. movieDetail. the above Statement (Statement) of cshtml specifies that the object is a type acceptable to the View. When MovieController is generated from the Model, the View corresponding to the Method in the Controller is also generated, @ model declaration is the @ model command automatically added by the system to force type conversion on the Model object, passing the data obtained by the Controller from the Model to the View Template, for example, in Detail. in cshtml, the code passes the data in the movie field through the MVCMovie of stronged typed. models. movie transfers data from the Controller to Detail. index in cshtml. in cshtml, check the code carefully to see how the data is displayed in View 1 @ model IEnumerable <MVCMovie. models. movie> <br> ...... <br> @ foreach (var item in Model) <br >{< br> <tr> <br> <td> <br> @ Html. displayFor (modelItem => item. title) <br> </td> <br> <td> <br> @ Html. displayFor (modelItem => item. releaseDate) <br> </td> <br> <td> <br> @ Html. displayFor (modelItem => item. genre) <br> </td> <br> <td> <br> @ Html. displayFor (modelItem => item. price) <br> </td> <br> <td> <br> @ Html. actionLink ("Edit", "Edit", new {id = item. ID}) | <br> @ Html. actionLink ("Details", "Details", new {id = item. ID}) | <br> @ Html. actionLink ("Delete", "Delete", new {id = item. ID}) <br> </td> <br> </tr> <br>} This line of statement is also created by vs automatically create

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.