我正在尝试搜索我的webgrid上的数据但是当我从下拉菜单中选择或键入textBox时,数据会消失。
我已关注此链接:http://www.mikesdotnetting.com/article/180/displaying-search-results-in-a-webgrid。
代码如下。请帮助。
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Payment Schedule";
var db = Database.Open("GroupCashFlow");
var query = "SELECT DISTINCT Company FROM PaymentSchedule ORDER BY Company";
var companies = db.Query(query);
query = "SELECT * FROM dbo.PaymentSchedule where Account like @0 and Company like @1";
var account = "%" + Request["account"] + "%";
var company = "%" + Request["company"] + "%";
var data = db.Query(query, account,company);
var columns = new[]{"Date","Company","Account", "Payee","Section","Category","Product","Currency","Amount","FX","USD","Comments"};
var grid = new WebGrid(data,columnNames: columns,rowsPerPage: 20);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<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>
</head>
<body>
<h1>Payment Schedule</h1>
<h2> </h2>
<form method="post">
<div id="grid">
account: <input type="text" name="company" value="@Request["account"]" />
Company: <select name="Company">
<option></option>
@foreach(var item in companies){
<option @(Request["Company"] == item.Company ? " selected=\"selected\"" : "")>@item.Company</option>
}
</select>
<input type="submit" />
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "header",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column(format: @<a href="~/EditDB?id=@item[0]">Edit</a>),
grid.Column("Date", format: (item) => string.Format("{0:MMM dd yyyy}", item.Date) ),
grid.Column("Company"),
grid.Column("Account"),
grid.Column("Payee"),
grid.Column("Section"),
grid.Column("Category"),
grid.Column("Product"),
grid.Column("Currency"),
grid.Column("Amount"),
grid.Column("FX"),
grid.Column("USD"),
grid.Column("Comments")
)
)
</div>
</form>
</body>
</html>
@section script{
<script type="text/javascript">
$(function(){
$('th a, tfoot a').live('click', function() {
$('form').attr('action', $(this).attr('href')) ;
return false;
});
});
</script>
}
答案 0 :(得分:1)
修复 - 我改变了这一部分:
account: <input type="text" name="company" value="@Request["account"]" />
要:
account: <input type="text" name="account" value="@Request["account"]" />