20 tips for ASP. net mvc 3 Development (2) [20 recipes for programming MVC 3]: Automatic Generation of controllers and views

Source: Internet
Author: User

Topics

Manage dynamic content through your website.

Solution

Bind the object framework to the database. You can use the controller and multiple automatically generated view interfaces to create, read, update, and delete (also called crud) data.

Discussion

Before defining controllers and views, you must create models, dbcontext, and data collection rules in advance (CodePriority ). In the following example, two classes will be created and a library list management function will be implemented. The first category contains the data definitions stored in the SQL express database. The second class will be a dbcontext class containing the dbset object of the book type. Create a model, right-click the models folder, and selectAdd->Class. Write book. CS in the class file name and replace the generated code with the following code:

 

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Data. entity;

Namespace Mvcapplication4.models
{
Public Class Book
{
Public Int Id { Get ; Set ;}
Public String Title { Get ;Set ;}
Public String ISBN { Get ; Set ;}
Public String Summary { Get ; Set ;}
Public String Author { Get ; Set ;}
Public String Thumbnail { Get ; Set ;}
Public Double Price { Get ; Set ;}
Public Datetime published { Get ; Set ;}
}

Public Class Bookdbcontext: dbcontext
{
Public Dbset <book> books { Get ; Set ;}
}

}

 

*Note: After saving book. CS, you must compile the entire project (shortcut: Ctrl + Shift + B) to continue the following operations.

Create the model and bookdbcontext class of the book, and then you can complete the controller and view. Start, right-click the Controller folder, and select "add controller" (1-1 ).

Example 1-1: Create a new Controller

Name the new controller as bookscontrollers. On the template tab, select "controllers that contain read/write operations and views (using Entity Framework )". Select the book class for the model class and the bookdbcontext class created earlier. Razor (cshtml) in the view options is the default option, so it remains unchanged. After entering the content, click "add" to create a file. (1-2)

 

Example 1-2: create a file

 

Entity Framework can create different operation methods and views for controllers based on existing databases. In large projects, these basic contents are made separately. For example, a good front-end web developer may not be an expert in database design. Therefore, database design requires professional personnel.

In the following example, you will connect to the previously created database and create an instance model for the "book" table. Select to use the old applicationProgramOr create a new application, depending on whether you select code first or database first.

After the program is created, right-click the models folder and choose "add"> "new item ". Type "entity" in the search box in the upper-right corner and select "ADO. NET Entity Data Model" from the search results. Change the file name to "bookmodel. edmx". Now you can use the Wizard to establish a database connection:

    1. Select "generate from database ";
    2. Click "create connection;
    3. Select Microsoft SQL server from the list, and click "continue ";,
    4. Select your SQL express database from the server name in the Connection Properties dialog box;
    5. After the connection, select the last table in the database automatically created by MVC in the database drop-down box, and then click "OK ".

 

*Note: Enter the server name in step 3. \ sqlexpress, step 4 should be the tempdb database. I don't know if I haven't seen it before. Of course, the "book" table mentioned below also does not exist, however, some operations in this chapter are not feasible.

Click Next to update the sqlexpress connection string in Web. config. Now, in the database selection dialog box, expand the table node and select the "book" table.

After you click "finish", a new object graph is created under the models directory. You must compile the solution before creating the controller. The project has been built, just as in the previous "code first" example. Right-click the Controller folder and choose "add"> "controller ".

The Controller created using this method still has the "book" model class, data context class, and entity instances that contain database connections.

In the future, we will use the code-first method to manually operate tables in the database, so that more attention can be paid to MVC itself.

References

ADO. NET Entity Framework overview Source Book Address Source Code

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.