ASP. net mvc music store-5. Create and edit forms through the bracket

Source: Internet
Author: User
Tags actionlink

In the previous chapter, we have obtained data from the database and displayed it. This chapter allows you to edit the data.

Create a storemanagercontroller

We will create a controller called storemanager. For this controller, we will implement it by using the scaffolding function provided in ASP. NET mvc3. In the Add controller window, select "CREATE", "Update", "delete", and "details" to add operation methods.

After clicking the Add button, you will see that the support mechanism of ASP. NET mvc3 has added a controller named storemanagercontroller to the controllers folder.

In the original article, a storemanagercontroller with local object framework variables is created.

However, in my vs2010, no Entity Framework variables were actually created. In the previous data access section, the Entity Framework on my machine was also manually downloaded and installed.

In the original article, create. cshtml, delete. cshtml, details. cshtml, Edit. cshtml, and index. cshtml views are created. These views use the strong album type.

However, the object type option is not selected in the dialog box. Therefore, in my case, the corresponding view is not actually created, let alone the strong type. However, these are actually minor issues.

The new storemanager controller contains the crud (create, read, update, and delete) controller action. In the original article, these controllers know how to use the context object of the album model and object framework to access data.

Since the object context object is not created, I have no actual data access here.CodeYes. In the following description, we will create this part manually.

Therefore, we manually add object context objects for data access to the Controller.

 
NamespaceMvcmusicstore. Controllers
{
Public ClassStoremanagercontroller: Controller
{
Mvcmusicstore. Models. musicstoreentities storedb
=NewMvcmusicstore. Models. musicstoreentities ();

 

Modify View

Here, we will manually generate the view and then modify it.

First, add a strong view for the index. Select create strong view in the dialog box, select model class, and select list in the bracket template, because we need to process the list of albums in the index view.

After the list bracket is used, the model type in the created view is generated as follows.

 
@ Model ienumerable <mvcmusicstore. Models. album>

 

Remember, although scaffolding can automatically generate code for us, it is only a standard ASP. net MVC code, just like this tutorial can save your learning time, the support can save you the time to manually create a controller and a strong type view, however, the specific details must be handled by yourself.

Therefore, Let's quickly edit the index view (/views/storemanager/index. cshtml) of storemanager ). This view displays the list of albums in a table, including the Public attributes of the albums and links for editing, details, and deletion. We want to delete the album artist link. We do not need to display this value. In the <Table> section of the view, delete the <TH> and <TD> elements related to albumarturl, the highlighted part of the code below.

In this way, the code of the index view should be as follows:

@ Model ienumerable <  Mvcmusicstore  . Models. Album  >  
@{
Viewbag. Title = "Index ";
}
< H2 >
Index </ H2 >
< P >
@ Html. actionlink ("create new", "CREATE ")
</ P >
< Table >
< Tr >
< Th >
Genre
</ Th >
< Th >
Artist
</ Th >
< Th >
Title
</ Th >
< Th >
Price
</ Th >
< Th >
</ Th >
</ Tr >
@ Foreach (VAR item in Model)
{
< Tr >
< TD >
@ Html. displayfor (modelitem => item. Genre. Name)
</ TD >
< TD >
@ Truncate (item. Artist. Name, 25)
</ TD >
< TD >
@ Truncate (item. Title, 25)
</ TD >
< TD >
@ Html. displayfor (modelitem => item. Price)
</ TD >
< TD >
@ Html. actionlink ("edit", "edit", new {id = item. albumid}) |
@ Html. actionlink ("details", "details", new {id = item. albumid}) |
@ Html. actionlink ("delete", "delete", new {id = item. albumid })
</ TD >
</ Tr >
}
</ Table >
First impression of store manager

Although the view has been modified, the index method does not provide data code to the view. You can modify the index method to increase the processing logic for obtaining data from the database.

PublicActionresult index ()
{
VaRAlbums = storedb. albums. Include ("Genre"). Include ("Artist");
ReturnView (albums. tolist ());
}

 

Now, runProgram, Browse/storemanager. Note that in routing configuration, the default action will use index. The modified view displays the list of albums with edits, details, and deletion links.

Note that editing, details, and deleting links must be manually created before they can be used.

Create these views in sequence. Place the cursor in the edit action method, right-click, and select add view from the pop-up menu.

In the create edit view dialog box, note that the bracket used is edit.

Similarly, modify the edit action method.

Note that two edit methods with the same name exist in the controller. The [httppost] label is used before the second method, this label indicates that when the request type is post, the method will process the request. Otherwise, the first edit method will be used for processing.

In ASP. in. Net MVC, the view State is no longer used. When users need to edit data, we first provide an editing form. The user can obtain this form through a hyperlink, such a request will be a GET request. When such a request arrives at the server, we return the Edit page to the client, allowing the user to edit the data.

In the form for editing data, we provide a form. The form submission method is set to post. When you submit a form, you can submit the entered data to the server. Since the submission method becomes the POST method, we can identify the request type on the server through the request submission method.

In this way, the controlller can provide actions with the same name to process user editing operations. Get actions are used to edit forms, post actions are used to obtain user submitted data. This method is widely used in ASP. net mvc.

The modified edit method is as follows:

 Public Actionresult edit ( Int ID)
{
Mvcmusicstore. Models. album = storedb. albums. Find (ID );
Return View (album );
}

//
// Post:/storemanager/edit/5

[Httppost]
Public Actionresult edit ( Int ID, formcollection collection)
{
Try
{
// Todo: Add update logic here
Mvcmusicstore. Models. album = storedb. albums. Find (ID );
If ( This . Tryupdatemodel <mvcmusicstore. Models. album> (album ))
{
Return Redirecttoaction ( " Index " );
}
Return View ();
}
Catch
{
Return View ();
}
}

 

Create a detailed content view. Note the bracket used.

Modify the processing logic of the details method accordingly.

 
//
//Get:/storemanager/details/5

PublicViewresult details (IntID)
{
Mvcmusicstore. Models. album = storedb. albums. Find (ID );
ReturnView (album );
}

 

In the create and delete view dialog box, pay attention to the bracket template used.

Similarly, there are two Delete methods, one for processing GET requests and the other for processing the requests after clicking confirm to delete.

Public Actionresult Delete ( Int ID)
{
Mvcmusicstore. Models. album = storedb. albums. Find (ID );
Return View (album );

}
//
// Post:/storemanager/delete/5

[Httppost]
Public Actionresult Delete ( Int ID, formcollection collection)
{
Try
{
// Todo: Add Delete logic here
Mvcmusicstore. Models. album = storedb. albums. Find (ID );
Storedb. albums. Remove (album );

Return Redirecttoaction ( " Index " );
}
Catch
{
Return View ();
}
}

 

Now, you can run the command to access/storemanager and get the following results.

Click the edit link to display an editing form with the album field.

Click the return List link at the bottom and click the details link. The details of a single album are displayed.

Again, go back to the list and click the delete link. Now a confirmation dialog box is displayed, showing the details of the album. Please confirm whether you really need to delete the album.

Click the delete button at the bottom to delete the album and return to the index list view. The album has been deleted.

Our work has not been completed yet. We have to start to process the crud operations on the controller and view.

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.