I use a WebGrid to display the result of:
select * from table where column like '**';
There is no problem displaying the result.
However, if I click the column name of the WebGrid to sort the results, I will get an error or there is nothing displayed on page.
What can I do to make the WebGrid sorting work ??
@{
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 ")
)
)
}
}