Asp.net MVC2 study Note 5-simple instance (movie) (I)

Source: Internet
Author: User

We have a basic understanding of route, Controller, and view. Today we will come to a simple instance-movie.

 

 

Before the start, let's briefly talk about the model. I believe you will use it when developing layers. The MVC model is the same as him,

Each model corresponds to a table in the database. In this way, the model attribute is used to access a column value recorded in each row of the table.

 

In the MVC Framework of Microsoft, you can write the model by yourself, or use the ADO. NET Entity Data Model, and the LINQ to SQL class. What we use for this instance today is

Entity Data Model. Next we will record the steps of this simple instance:

 

1. Create a movies table in the database:

Field: Id int, -- auto-increment primary key

Movie_name nvarchar (50), -- movie name

Release_date datetime, -- release time

 

 

Ii. Create an Asp.net MVC2 Project

 

Here we can create Asp.net MVC 2 web application (of course, we can also create an empty MVC project), Project name: Movie

 

  

 

After the project is created, we can delete unused controllers, models, and views.

 

3. Create a model Layer Model

 

Here we use the object data model to complete our model.

 

Right-click the models folder and choose --> Add item. Select ADO. NET Entity Data Model and name: mvoie.(My note: Movie)

 

  

 

Select generate from database below

 

  

 

Create a database connection and connect to our test database

 

  

 

Select the expected table (movie). If the database has a view or a stored procedure, you can select the table,

Model namespace: Models

Click Finish ..

 

  

 

Now we can see the physical data model design interface we have created. Isn't that the name will be automatically changed to the singular number? Why didn't I change it? Haha, maybe it's the Chinese version of vs. The reason is unknown... Let's change it manually .. Change movies to movie.(My note: Here is the singular, and it is also the Chinese version of)

Haha, well, our model has been created so far. Do you think it is very convenient for the real data model ?...

 

 

 

4. Create a control layer-Controller

 

After the above model is created, we can build our project first (if the test project is automatically generated and the default controller is deleted, an error will be reported in the test project, in this case, we can delete the Controller Test file in the test project)

 

Next we will create a new controller named moviescontroller.

Right-click the controllers folder and choose --> Add --> controller. Enter the name moviescontroller.Check "Add action methods for create, update, and details scenarios ",Let's take a look at it to understand the general meaning, that is, "add the creation, update, and details methods"

Click Add ..

 

  

  

After adding, we can see a controller with the function framework of adding, editing, and details. Note that it is only a framework and does not have actual functions. We need to complete the logic code by ourselves.

 

  

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. MVC; </P> <p> namespace movie. controllers <br/>{< br/> public class moviescontroller: controller <br/>{< br/> // <br/> // get: /movies/</P> <p> Public actionresult index () <br/>{< br/> return view (); <br/>}</P> <p> // <br/> // get: /movies/details/5 </P> <p> Public actionresult details (int id) <br/>{< br/> return view (); <br/>}</P> <p> // <br/> // get:/movies/create </P> <p> Public actionresult create () <br/>{< br/> return view (); <br/>}</P> <p> // <br/> // post: /movies/create </P> <p> [httppost] <br/> Public actionresult create (formcollection collection) <br/>{< br/> try <br/>{< br/> // todo: add insert logic here </P> <p> return redirecttoaction ("Index "); <br/>}< br/> catch <br/> {<br/> return view (); <br/>}</P> <p> // <br/> // get: /movies/edit/5 </P> <p> Public actionresult edit (int id) <br/>{< br/> return view (); <br/>}</P> <p> // <br/> // post: /movies/edit/5 </P> <p> [httppost] <br/> Public actionresult edit (int id, formcollection collection) <br/>{< br/> try <br/>{< br/> // todo: add update logic here </P> <p> return redirecttoaction ("Index "); <br/>}< br/> catch <br/> {<br/> return view (); <br/>}< br/>

 

 

Here we use the index method to display the movie list function (we need to display data, of course, first insert several pieces of test data in the database ..), Now we can modify the method in the index,

 

// <Br/> // get:/movies/</P> <p> Public actionresult index () <br/> {<br/> testdbentities _ dbentities = new testdbentities (); // create a database entity </P> <p> return view (_ dbentities. movieset. tolist (); // return the view with the movie set <br/> // You can also manually specify viewdata. model = _ dbentities. movieset. tolist (); <br/>}< br/>

 

 

5. View Layer

Now let's create a view for the index method in the controller. The method is:

Right-click the index and choose -- add view;

  

In the following panel, we select "create a strong view". What is a "strong view"? As the name suggests, it has a "strong" type. What is the type? Now, let's select the one we created below.Model -- movie. models. movie (I note: I understand the type here. I don't know if there is any error. The first movie is the project name, and the second models is the project folder name models, the last movie is the object class file name movie. edmx. Remember that it is not the class name in the file)This is its type ..

 

In the View context view, select list (because we need to create a view that displays the movie list)

  

 

After adding is complete, we have basically finished displaying the movie list page, isn't it easy? Okay, okay. Let's get F5!

We found that the home page is still the default home/index, and some people deleted the original home directory, prompting that the page could not be found. Haha, it doesn't matter, because weGlobal. asaxThe default controller is set in it. Remember? You can change the new {controller = "home", Action = "Index", id = ""}New {controller = "Movies", Action = "Index", id = ""}You can .... F5 .. Is it possible to see our movie list? Haha ....

 

Okay, now the list is basically displayed. Let's take a look at the page content generated by it... Why? Nothing. After we select the list type, he will give us a foreach table, which is quite convenient. Oh, of course, we can also choose not the list type, select an empty type and write it by ourselves... The operations in the view are as follows. Some HTML code can be changed if you want to modify it...

 

 

 

Now, I will write it here today. The next article will specifically complete the edit, add, delete, and display details functions (I believe everyone should think about it, huh, huh ), I hope this article will be helpful to beginners ....

 

From: http://www.cnblogs.com/bboy/archive/2010/01/25/1655389.html

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.