<h1>Delete a movie</h1>
<p>Are you sure you want to delete the movie <strong>@Movie.Name ?</strong></p>
<form action="" method="post">
<input type="submit" value="Yes"/>
<input type="button" value="No" onclick="window.location = dataMovies.cshtml" />
</form>
就像EditMovie.cshtml网页一样,此网页将被调用和传递一个参数,该参数为要删除的电影的ID。在文本“Are you sure you want to delete the movie @Movie.Name?”中,Movie.Name的值将由服务器插入。所以,我们需要告诉服务器如何获取此值:为此,跟以前一样,在网页顶部添加一些Razor代码来获取输入参数。
@{
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;
}
DELETE FROM Favorites WHERE ID=2,当用户单击“Yes”按钮时,将提交该窗体并发生删除操作,我们可以在回发时执行此任务,如下所示:这将删除电影,将我们重定向回列表网页,以便我们看到它消失了。
下面是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");
}
}
<h1>Delete a movie</h1>
<p>Are you sure you want to delete the movie <strong>@Movie.Name ?</strong></p>
<form action="" method="post">
<p><input type="submit" value="Yes"/>
<input type="button" value="No" onclick="window.location = 'dataMovies.cshtml'" /></p>
<br/>
</form>
@{
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>