So far, you've created a data-driven favorite movie list, set its style, added the ability to add new movies and edit existing movies in the database. The next step in creating this application is to provide the user with the ability to delete records from the database.
Guide: Microsoft WebMatrix is a free tool that you can use to create, customize, and publish Web sites on the Internet.
WebMatrix makes it easy for you to create sites. You can start with an open source application (such as WordPress, Joomla, DotNetNuke, or Orchard), and WebMatrix will handle the tasks of downloading, installing, and configuring these applications for you. Or you can use many of the built-in templates to write your own code that will help you get started quickly. Whatever your choice, WebMatrix provides everything you need to run your site, including Web servers, databases, and frameworks. By using the same stack that you will use on the web host on your development desktop, the process of getting your site online is easy and smooth.
You can download it from Http://web.ms/webmatrix.
Now you can learn to use WebMatrix, CSS, HTML, HTML5, ASP, SQL, database, and more, and how to write simple Web applications in just a few hours. The contents are as follows:
When it comes to web application development and data, you may have heard of the term crud. CRUD represents create, Retrieve (retrieve), update (update and delete), which accurately outlines what you do with WebMatrix.
Add Delete page
First, create a new cshtml Web page and name it deletemovie.cshtml.
Replace the HTML with the following:
This code creates a basic form with 2 buttons, the submit button triggers the HTTP POST (just like the edit page in the previous section), and the other button redirects you back to the movie list when clicked.
Just like the editmovie.cshtml Web page, this page will be called and passed a parameter, which is the ID of the movie to be deleted. In the text "is sure you want to delete the movie @Movie. Name?" , the value of Movie.name is inserted by the server. So, we need to tell the server how to get this value: for this, as before, add some razor code at the top of the page to get the input parameters.
@{ var id=request["id"]; var sqlselect = "Select * from Favorites where id=@0"; var db = Database.open ("Movies"); var Movie = db. Querysingle (sqlselect,id); var moviename=movie.name; }
Here you can see that the parameter is passed as "id" to the Web page (using deletemovie.cshtml?id=<whatever>), which is used to find a specific movie. Executes a query against the database and gets a record of the movie. You can now get the name of the movie and render it when the page is rendered.
Run Deletemovie.cshtml?id=<something> You will see this screen, provided that <something> is a valid ID in the database:
If you click the No button, you will be redirected back to the datamovies.cshtml Web page. If you click Yes, nothing happens because the code has not been written to handle the delete operation.
To delete a record from the database, use the delete SQL command. This can be used with the following syntax for delete from <Table> WHERE <field>=<value>, so if you want to delete id=2 movies, you can write:
Delete from Favorites where id=2, when the user clicks the "Yes" button, the form is submitted and a delete operation occurs, and we can perform this task on postback, as follows: This will delete the movie and redirect us back to the list page so that we see it disappears.
The following is the complete code for deletemovie.cshtml:
@{ var id=request["id"]; var sqlselect = "Select * from Favorites where id=@0"; var db = Database.open ("Movies"); var Movie = db. Querysingle (sqlselect,id); var moviename=movie.name; if (IsPost) { var sqldelete = "DELETE from Favorites WHERE id=@0"; Db. Execute (sqldelete,id); Response.Redirect ("datamovies.cshtml"); } }
Delete a page from a movie list call
Now that we have a valid delete page, we connect it to the movie list page so that the user can delete the item from the list and request that it be deleted.
On the Movie list page, simply add a hyperlink to each list item, where the hyperlink links to the Deletemovie.cshtml Web page and passes the current movie's ID to it.
The following is the complete code for datamovies.cshtml:
@{ var db= database.open ("Movies"); var SQLQ = "Select * from Favorites"; var data = db. Query (SQLQ); } <div id= "Movieslist" > <ol> @foreach (var row in data) { <li> <a href= "@row. name>editmovie.cshtml?id= @row. ID > @row. Name, @row. Genre, @row. Releaseyear </a> <a href= "deletemovie.cshtml?id= @row. Id" >Delete</a> </li> } </ol> <a href= "addmovie.cshtml" >add a new movie</a> </div>
Run this page and see the workflow that was deleted. First of all, this is the new datamovies.cshtml:
The above is WebMatrix Advanced Tutorial (8): Create delete data page content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!