Learn ASP (v)-My first ASP. NET MVC Curd page

Source: Internet
Author: User
Tags actionlink

In the previous article we have created the entity class, in this article I will create a new controller class--bookcontroller, using Bookcontroller to curd the data in the books table, and use the view template to display the book data information you are querying in the browser.


First, add the Controller class

In Visual Studio , in Solution Explorer , right-click the Controllers folder and create a new Bookcontroller controller class. Such as.

Visual Studio will pop up a Add Controller dialog box (for example).

Fill in the following data in the dialog box

• Controller name: Bookcontroller. (Can be modified as the default Default1controller, modified to Bookcontroller.) )
• Templates: Includes read/write operations and view MVC controllers (using the entity Framework).
• Model class: Book (Mvcapplication1.models).
• Data Context class: Bookdbcontext (Mvcapplication1.models).
• View: Razor (CSHTML). (the default setting.) )

After filling in the above data, click the "Add" button with the left mouse button. Visual Studio quickly creates the following files and folders:
1) Create a BookController.cs file in the Controllers folder in the project. As in the 1 places.
2) Create a book folder in the Views folder of the project. Also create the following files in the book folder create.cshtml, delete.cshtml, details.cshtml, edit.cshtml, and index.cshtml. As in the 2 places.


3) ASP. NET MVC 4 automatically creates crud (create, read, update, and Delete) operations methods and views for the database. Automatic creation of CRUD operations methods and views by Visual Studio is called scaffolding. So that we don't need to modify anything, there's a full-featured Web application that lets you create, query, modify, and delete book data.
4) Run the application in Visual Studio. , the browser requests that the Http://localhost:36878/Book be routed to the book Controller's default index action method. In other words, the browser request Http://localhost:36878/Book is actually the same as the request Http://localhost:36878/Book/Index. What data is not displayed in the browser, because now the books table in the database is an empty table with no data. Such as.

Ii. Creation of Books
1) with the left mouse button, click on the "Create new" link, the browser will navigate to the new page, on this page enter some data about the book, and then click the "Create" button. such as 1, with Figure 2.

Figure 1

Figure 2

2) when using the left mouse button to click "Create" buttons, the browser will send the form data to the server, the server program will just fill in the book information saved to the database. The browser will then automatically redirect to the URL of the book list (http://localhost:36878/book), where you can see the newly created book data that already exists. Such as.

Iii. Editing of Books

Make changes to the book data you just created. There are three links on the last side of a book record, namely Edit,Details,deltet. Such as.

1) Click the "Edit" link with the left mouse button. The browser will automatically go to the edit screen. After modifying the data in the edit page, click the "Save" button to save the modified data to the database. Such as.

2) 2 Click the "Details" link with the left mouse button. The browser will automatically go to the details screen. Such as. Figure 1, Figure 2.

Figure 1

Figure 2

Iv. explanation of automatic code generation for ASP.
Open the Controllers\ BookController.cs file in Solution information manager in Visual Studio to view the index method that is automatically generated by Visual Studio. The code is shown below.

 Public class bookcontroller:controller{    privatenew  bookdbcontext ();     //    //  //public      actionresult Index ()    {       return View (db. Books.tolist ()); }}

Note This line of code.

private Bookdbcontext db = new Bookdbcontext ();

This line of code is instantiating a Bookdbcontext class object. As described earlier. You can use the data in the Books table in the database to query, modify, and delete.

When the browser makes a request to the book Controller, the default method (Index) in the controllerreturns all rows from the data in the Books table in thedatabase. and pass the result to the Index View, which is then rendered in the browser so that the user sees it.


v. Strongly typed objects and @model keywords
in the above section of this article, we talk about how to pass data or objects through a controller to a view template that uses the ViewBag object. The ViewBag is a dynamic object that provides a way to pass information to a view that can be easily bound at a later stage.
ASP. NET MVC also provides the ability to strongly type data or to pass an object instance to a view template. This strongly typed object makes it easier for Visual Studio to check code when it compiles, and to have richer IntelliSense in the Visual Studio Editor. the scaffolding mechanism in Visual Studio is to use this approach with Bookcontroller class and view templates.
View the automatically generated Details method in the controllers\ BookController.cs file . The code for the Details method is shown below.

 Public ActionResult Details (int0) {      = db. Books.find (ID);             if NULL )            {                return  httpnotfound ();            }             return View (book);}

book An instance of an entity is passed to details view. See views\book\details.cshtml File.  
@model statement, you can specify the type of object The view expects to get. When you create a bookcontroller The Controller class, visual studio is automatically included in the details.cshtml Span style= "FONT-FAMILY:CALIBRI;" > @model

@model MvcApplication1.Models.Book

This @ Model instruction allows you to pass a strongly typed access controller to the book Class of the view's model object. For example, in the details.cshtml template, the code passes through each book field of Displaynamefor and displayfor HTML-assisted with a strongly typed model object. Creating and editing methods and view templates can also be accessed through the book's Model objects.
View the index.cshtml view template and locate the index method in the BookController.cs file. Notice how the code creates a list object, which is encapsulated with view, which eventually passes the list of books from the controller to the view. The code is as follows.

 Public actionresult Index ()    {       return  View (db. Books.tolist ()); }

When you create a Bookcontroller controller, Visual Studio automatically adds the following @model statement at the top of the index.cshtml file:

@model ienumerable<mvcapplication1.models.book>

This @ Model directive allows you to access the list of books that the controller passes to the view by using the strongly typed model object. For example, in the index.cshtml template, the following code accesses the Model object through a strong type in the loop . The code is as follows.

@foreach (var item in Model) {<TR>        <TD>@Html. displayfor (ModelItem = Item. Category)</TD>        <TD>@Html. displayfor (ModelItem = Item. Name)</TD>        <TD>@Html. displayfor (ModelItem = Item. NumberOfCopies)</TD>        <TD>@Html. displayfor (ModelItem = Item. Authorid)</TD>        <TD>@Html. displayfor (ModelItem = Item. Price)</TD>        <TD>@Html. displayfor (ModelItem = Item. Publishdate)</TD>           <TD>@Html. displayfor (model = Item. Rating)</TD>        <TD>@Html. ActionLink ("edit", "edit", new {Id=item.            BookID}) | @Html. ActionLink ("Details", "details", new {Id=item.            BookID}) | @Html. ActionLink ("delete", "delete", new {Id=item. BookID})</TD>    </TR>}

Because model objects are strongly typed (such as IEnumerable <book>), the type of each item object in the loop is book. In addition to other benefits, this means that you will get compile-time code checking functionality and full IntelliSense support . Such as.

Run the program, and then you will see the data as shown.

Xiao Kee: Recently work a little busy, so the update is a little late. Try to finish it.

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.