. Net MVC2 exercise notes

Source: Internet
Author: User
Tags visual studio 2010

This tutorial is based on Visual Studio 2010.

1. Create a project and select "ASP. net mvc 2 web application" (not its empty application)
Name: myuser
Location: D: \ msproject \
Solution name: myapps
In the next step, you will be prompted whether to create a test project. By default, the project file is generated.
1) about the test project: If you right-click the solution and select build, an error is reported because the test project has not been referenced by the web project.
Right-click the reference under the test project (myapps. Tests) and select "add reference". The default project displays your new project in the pop-up window. Click "OK" to reference the project.
After the reference is added, right-click the solution and generate it. No error is reported.
2) then, "run the test (CTRL + R, A) in the solution", the second button in the test button combination, and the test project will be tested in the lower box.
About whether it is created successfully: "debug"-> "start debugging" (Press F5), the default page will pop out.

2. Create a database and use SQL express to create a test table
1) Right-click solution, select Add new item, select "data" on the left of the pop-up window, and select "SQL Server database" on the right, with the name myapps. MDF. When saving, you will be prompted to save it to the app_data folder and select "yes" to finish adding.
If the instance fails to be created, check whether your SQL express service is enabled.
2) double-click myapps. MDF in the project on the right to open the connection to the database. Right-click the table and select Add new table. The page for adding columns is displayed.
Select auto-increment in the userid int attribute
Username nvarchar (50)
Nickname nvarchar (50)
Phone nvarchar (50)

3. Create a model layer that involves: Ling to SQL, Entity Framework 4, object class creation, and interface creation

(1) Right-click models and choose "add project"> "data"> "ADO. NET Entity Data Model", click "add", and select "generate from database" in the pop-up window to proceed to the next step.
----Select the database you just created from the drop-down menu. Click Next and all the table information of the database will be displayed. You can select tables, views, and stored procedures. We recommend that you select all of them;
----Select "confirm the singular and plural form of the Object Name generated", and then generate the object model;
----The myuser. edmx relationship graph page is displayed. myuserentities in the graph are registered in the web. config file;
----In this object relationship diagram, there will be names and object set names, both automatically named the same as the table name;
----For example
Name: user (default)
Object Set Name: Users (check the result of "determining the singular and plural form of the Object Name generated)

(2)Add the corresponding data model access interface class,Right-click "models" and choose "code"> "interface" to create an interface file.
Add some basic methods to the interface file:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace myuser. Models
{
Interface imyuserdao
{
Public iqueryable <myuser> findall ();
Public iqueryable <myuser> findmyuser ();
Public myuser getmyuser (int id );
Public void add (myuser user );
Public void Delete (myuser user );
Public void save (myuser user );
}
}

3)Implement interfaces for data access objects,VS is not as convenient as myeclipse. You can find and select the interface you want to inherit when creating a new version.
To implement the interface layer, you must manually write the methods in the inherited interface and the implemented interface.
An error occurs during this process:
Namespace myuser. Models
{
Public class myuserdao: myuser. Models. imyuserdao // right-click the interface and select the implementation Interface
{
}
}
Correct:
Namespace myuser. Models
{
Public class myuserdao: imyuserdao // right-click the interface and select the implementation Interface
{
}
}
 
The steps here are mainly divided into two parts: one is to define a new data access object, the other is to implement an interface and operate with a Data Access Object
For example:
// Declare the data access interface
Myuserentities DB = new myuserentities (); // defined in myuser. Designer. CS in myuser. edmx

Public iqueryable <myuser> findall ()
{
Return dB. users;
}

4. Create the Controller layer. Note that the routing (URL rewriting) of this layer is in the global. asax file.

(1) Right-click the Controller folder and choose create controller class named usercontroller. CS.
(2) There is an index () Action by default, and a detail () action is added to call the Database Access Object implementation function in the method.
(3) Right-click View and choose add view. The name is notfound. Then, the notfound. aspx is automatically created in the view directory.
Generally, we generate a View File Based on the corresponding method, that is, place the cursor in the row where the method is located, right-click and choose add view.
In this way, the generated View File is automatically placed in the user directory. Only in this way can the path be correctly accessed.

Note! We can use a mandatory view to add, delete, modify, and query:
1) Right-click "action" to add a new view, and select "Create strong view" in the pop-up window.
2) If this option is selected, specify the corresponding model in the View data class and select a specific operation in the View content, such as the details of this method.
3) the system will automatically generate a detail. apsx page corresponding to the fields in the model class, which is very convenient.
4) We can also generate a list of strong views in the index method, so that we have a simple index. apsx page.

5. Pay attention to generation.

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.