我使用 WebGrid 来显示以下结果:
select * from table where column like '**';
显示结果没有问题。
但是,如果我单击 WebGrid 的列名对结果进行排序,则会出现错误或页面上没有显示任何内容。
我该怎么做才能使 WebGrid 排序工作?
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Test";
var value = "";
var sqlquery = "";
var isvalid = true;
var searchaskey = "";
var grid = new WebGrid();
if(IsPost)
{
value = Request.Form["input"];
if(string.IsNullOrEmpty(value))
{
isvalid = false;
}
if (isvalid)
{
searchaskey = "'%" + value + "%'";
sqlquery = "select * from joblist where JobCategory like " + searchaskey;
var db = Database.Open("StarterSite");
grid = new WebGrid( db.Query(sqlquery));
}
}
}
<form method="post" action="Test.cshtml">
@value
@sqlquery
<div>
<input name="input" type="text" value=@value>
<button type="Submit">Submit</button>
</div>
</form>
@{
if(IsPost)
{
@grid.GetHtml(
columns:grid.Columns(
grid.Column(columnName : "JobTitle",header:" Job Title "),
grid.Column(columnName : "JobCategory",header:" Job Category "),
grid.Column(columnName : "CompanyName",header:" Company Name "),
grid.Column(columnName : "PostedOn",header:" Post Date ")
)
)
}
}