私は次のコードを持っています:
@{
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>
長さは気にせず、かみそりのコードだけに注目してください。簡単に言うと、2つのテキストボックスと1つのボタンがあります。ユーザーが「タイトル」または「ジャンル」のいずれか、あるいは両方の検索ボックスに値を入力したときに、テーブルを表示したいと思います。ただし、次のエラーが発生します。