Asp.net mvc3-from controller to Model

Source: Internet
Author: User

The database structure has been created in the database and model section. In this section, we will create a new bookscontroller class and writeCodeTo obtain the data in the database and display it in the browser through the view template.

Right-click the controllers folder and click the "controller" menu under the "add" menu. A "add controller" dialog box is displayed:

Add Controller

In this dialog box, name it bookscontroller and click Add. This dialog box is closed. In Solution Explorer, a file named bookscontroller. CS is added to the controllers folder and is open. Let's update the index method in the bookscontroller class to get the book list.

Note that to reference the booksrepository class we created earlier, you need to append using mvcbooks. models to the file header;

This code implements a LINQ query to obtain information about all books. We also need a view template to display this list of books, so right-click the index method, click "add view" to add a view.

Because we need to pass a movie class to the view, the operation on the "add view" dialog box is different from the previous operations on this dialog box in this tutorial, in the past few times, we directly click the Add button to create a blank view template. However, this time we want vs2010 to automatically create a strong view with some default processing, therefore, select the "Create strong view" check box and select "movie (mvcbooks. models) (if this class does not exist in the model class, click "generate mvcbooks" under the debugging menu to generate this class), select "list" in the context template drop-down box ", finally, check the "reference script" check box:

Add a strong view

Click the Add button. vs2010 automatically generates a view and automatically adds the Code required to display the book list in the View File. Here, we first use the same method as the title in the view used to modify the helloworld controller.

The following code list shows the complete code in the modified view. In this Code, we changed the format string of the releasedate attribute from the original "{0: g}" to "{0: d} "(change long date to short date), change the format string of the price attribute from the original" {0: f} "to" {0: c} "(the float type is changed to the currency type ).

In addition, modify all the text in the list title to a Chinese name.

Enter http: // localhost: XX/books in the browser. The running result is as follows:

Strong model and @ model keywords

In the previous article of this tutorial, we introduced a controller that can use viewbag objects to pass data or objects to the view template. Viewbag is a dynamic object. It provides a convenient method for later binding to transfer information from the Controller to the view.

ASP. net mvc also provides a method to transmit data or objects to a view template using a strong type. This type of strong method provides a wealth of Intelligent Input prompt information during editing and a very good check at compilation for your encoding process. Next we will combine this method with our books controller (bookscontroller) and view template (index. cshtml.

Note that in the index method of our bookscontroller controller, we passed in a parameter when calling the view () method. This line of code indicates passing a books list from the Controller to the view.

Return view (books. findallbooks ());

By using the @ model statement in the header of the view template file, the view template can identify whether the object type in the input parameter is the object type required by the view template. Remember that when we create the template used by this books controller, we select the "Create strong view" check box in the "add view" dialog box, in the model class drop-down box, select "bookinfo (mvcbooks. models), select "list" in the stent template drop-down box ". Therefore, vs2010 automatically adds the following statements to the first line of our view template file.

@ Model ienumerable <mvcbooks. Models. bookinfo>

The @ model keyword allows us to directly access the list of bookinfo classes passed in the Controller class by using a strong "model" in the view template. For example, in our index. cshtml view template, we can use the foreach statement to traverse this strong model and access every bookinfo object in it.

Because the "model" is strongly typed (ienumerable <bookinfo>), every item in the "model" is also a strongly typed bookinfo object, you can directly access every attribute of this object. This also means that we can check our code during compilation and use the smart input prompt information provided by the code editor when writing code.

You can now access the bookinfo data table in the database and have a simple page to display the content in the table. In the next section, we will add a method for appending data and a view for appending data, and append some data to the database.

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.