@{
var db = Database.Open("WebPagesMovies") ;
var selectCommand = "SELECT * FROM Movies";
var searchTerm = "";
if(!Request.QueryString["searchGenre"].IsEmpty() ) {
selectCommand = "SELECT * FROM Movies WHERE Genre = @0";
searchTerm = Request.QueryString["searchGenre"];
}
if(!Request.QueryString["searchTitle"].IsEmpty() ) {
selectCommand = "SELECT * FROM Movies WHERE Title LIKE @0";
searchTerm = "%" + Request["searchTitle"] + "%";
}
var selectedData = db.Query(selectCommand, searchTerm);
var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3);
}
<!DOCTYPE html>
<htmllang="en">
<head>
<metacharset="utf-8"/>
<title>Movies</title>
<styletype="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>
</head>
<body>
<h1>Movies</h1>
<formmethod="get">
<div>
<labelfor="searchGenre">Genre to look for:</label>
<inputtype="text"name="searchGenre"value="@Request.QueryString["searchGenre"]"/>
<inputtype="Submit"value="Search Genre"/><br/>
(Leave blank to list all movies.)<br/>
</div>
<div>
<labelfor="SearchTitle">Movie title contains the following:</label>
<inputtype="text"name="searchTitle"value="@Request.QueryString["searchTitle"]"/>
<inputtype="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>
其中的:
var selectedData = db.Query(selectCommand, searchTerm);
是查询语句,searchterm是参数
selectCommand = "SELECT * FROM Movies WHERE Title LIKE @0";
参数依次为@0,@1
var grid = new WebGrid(source: selectedData, defaultSort: "Genre", rowsPerPage:3);
defaultSort 默认排序方式