我有以下代码:
@{
var db = Database.Open("WebPageMovies") ;
var selectCommand = "SELECT * FROM Movies";
var searchGenre = "";
var searchTitle = "";
var selectedData = "";
// search by Genre
if(!Request.QueryString["searchGenre"].IsEmpty() ) {
selectCommand = "SELECT * FROM Movies WHERE Genre = @0";
searchGenre = Request.QueryString["searchGenre"];
}
// searching by title ( but any word or words that match a title will work).
if(!Request.QueryString["searchTitle"].IsEmpty()){
selectCommand += " AND Title LIKE @1";
searchTitle = "%"+ Request.QueryString["searchTitle"] + "%";
}
// if both textboxes are empty, then the following is dispayed
if(searchGenre != null)
{
selectedData = db.Query(selectCommand, searchGenre,searchTitle);
}
else
{
selectedData = db.Query(selectCommand,searchTitle);
}
var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3);
}
<!DOCTYPE html>
<html lang="en">
<head>
<style type="text/css">
.grid { margin: 4px; border-collapse: collapse; width: 600px; }
.grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; }
.head { background-color: #E8E8E8; font-weight: bold; color: #FFF; }
.alt { background-color: #E8E8E8; color: #000; }
</style>
<meta charset="utf-8" />
<title>Movies</title>
</head>
</head>
<body>
<h1>Movies</h1>
<form method="get">
<div>
<label for="searchGenre">Genre to look for:</label>
<!-- in order for the textbox to rememebr what search value was entered, we need to provide a value attribute with that search value in our HTML-->
<input type="text" value="@Request.QueryString["searchGenre"]" name="searchGenre" value="" />
@*<input type="Submit" value="Search Genre" /><br/>*@
(Leave blank to list all movies.)<br/>
</div>
<div>
<label for="SearchTitle">Movie title contains the following:</label>
<input type="text" name="searchTitle" value="@Request.QueryString["searchTitle"]" />
<input type="Submit" value="Search Title" /><br/>
</div>
</form>
<div>
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("Title"),
grid.Column("Genre"),
grid.Column("Year")
)
)
</div>
</body>
</html>
不用担心它的长度,只关注剃须刀代码。简而言之,我有两个文本框和一个按钮。当用户在“标题”或“流派”或两个搜索框中输入值时,我想显示一个表格。但是,我收到以下错误: