问题描述
两个页面。Search.cshtml用来接收搜索关键字SearchResult.cshtml用来显示搜索内容。在这个页面里使用了webgrid。显示的搜索结果也正常。但是如果点击数据列的名字进行排序的时候就报错。我自己大概意识到跟页面刷新之后select语句当中的参数缺失了有关系。当使用select*fromtable;
这样的语句时候。显示和排序都正常。附加了like之后显示正常。排序功能不正常。而且会报错。select*fromtablewherecolumnlike‘xxx’;
但是不知道如何解决。代码如下。求指导。Search.cshtml@{Layout="~/_SiteLayout.cshtml";Page.Title="Search";}<formmethod="post"action="SearchResult.cshtml">Search:<inputname="searchtext"type="text"style='width:20%'><selectname='searchindex'><optionvalue='JobCategory'selected>JobCategory</option><optionvalue='JobList.JobTitle'>JobTitle</option><optionvalue='JobID'>JobID</option><optionvalue='3'></option><optionvalue='4'></option>s<optionvalue='5'></option><optionvalue='6'></option></select><inputtype="submit"value="Search"name="Search"/></form>
SearchResult.cshtml@{Layout="~/_SiteLayout.cshtml";Page.Title="SearchResult";//valuesvarsearchtype="";varsearchtext="";varsearchindex="";varsearchaskey="";intsqlcount=0;varsqlresult="";varsqlquery="";//testvaluesvarsearchtest="";if(string.IsNullOrEmpty(searchtext)){searchtext=Request.Form["searchtext"];}if(string.IsNullOrEmpty(searchindex)){searchindex=Request.Form["searchindex"];}if(string.IsNullOrEmpty(searchaskey)){searchaskey="'%"+searchtext+"%'";}sqlquery="select*fromjoblistwhere"+searchindex+"like"+searchaskey;@sqlquery;vardb=Database.Open("StarterSite");vargrid=newWebGrid(db.Query(sqlquery));searchtest="searchteset="+sqlcount.ToString()+""+searchtype+""+searchtext+""+searchindex+""+searchaskey;@searchtest;@grid.GetHtml(columns:grid.Columns(grid.Column(columnName:"JobTitle",header:"JobTitle"),grid.Column(columnName:"JobCategory",header:"JobCategory"),grid.Column(columnName:"CompanyName",header:"CompanyName"),grid.Column(columnName:"PostedOn",header:"PostDate")))}
另外附上一个WebGrid显示和排序功能都正常的页面代码。@{Layout="~/_SiteLayout.cshtml";Page.Title="JobList";vardb=Database.Open("StarterSite");varselectJob="selectUserId,JobTitle,JobCategory,CompanyName,PostedOnfromJobList";vargrid=newWebGrid(db.Query(selectJob));}<p>JobList</p>@grid.GetHtml(columns:grid.Columns(grid.Column(columnName:"JobTitle",header:"JobTitle"),grid.Column(columnName:"JobCategory",header:"JobCategory"),grid.Column(columnName:"CompanyName",header:"CompanyName"),grid.Column(columnName:"PostedOn",header:"PostDate")))
解决方案
本帖最后由 zzytiger 于 2012-05-09 02:01:22 编辑
解决方案二:
继续求解
解决方案三:
还是无人解答啊。。。