下面模板为同时搜索news和product表的数据。
@{
string kw = StringHelper.Format<string>(HttpContext.Current.Request.QueryString["kw"]);
string[] pagePathArr = Html.PagePath().Split('/');
int currentPage = StringHelper.Format<int>(pagePathArr[pagePathArr.Length - 2]);
if (currentPage <= 0)
{
currentPage = 1;
}
int pageSize=24;
PageInfo pageInfo = new PageInfo()
{
PageSize = pageSize,//每页显示24条数据
CurrentPage = currentPage //当前页码,系统预设
};
List<dynamic> infoList = new List<dynamic>();
Dictionary<int, int> columnIds = new Dictionary<int, int>();
Dictionary<int, string> columnNames = new Dictionary<int, string>();
int columnId = 0;
int i=1;
int recordCount=0;
if (!string.IsNullOrEmpty(kw))
{
//获取总数据量,数据多建议不统计。
string countSql=" SELECT count(id) as co ";
countSql += " FROM (";
countSql += " SELECT id FROM [news] where state=1 and isSubColumnData=0 and title like @kw";
//多表连接
countSql += " UNION ALL SELECT id FROM [product] where state=1 and isSubColumnData=0 and title like @kw";
countSql += ") AS sub_query";
var countData= Html.SqlQueryFirst(countSql,new { kw = "%" + kw + "%" });
recordCount=countData.co;//获取总记录数
pageInfo.RecordCount=recordCount;
int pageCount = (recordCount % pageSize == 0) ? (recordCount / pageSize) : (recordCount / pageSize + 1);
pageInfo.RecordCount = recordCount;
pageInfo.PageCount = pageCount;
//查询数据
string sql=" SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY thedate desc) AS row_num";
sql += " FROM (";
sql += " SELECT id,columnId,title,thedate,'news' as [name] FROM [news] where state=1 and isSubColumnData=0 and title like @kw";
//多表连接
sql += " UNION ALL SELECT id,columnId,title,thedate, 'product'as [name] FROM [product] where state=1 and isSubColumnData=0 and title like @kw";
sql += ") AS sub_query";
sql+=") AS numbered_rows WHERE row_num BETWEEN "+((currentPage-1)*pageSize+1)+" AND "+currentPage*pageSize;
infoList = Html.SqlQuery(sql,new { kw = "%" + kw + "%" }).ToList();
}
ViewDataDictionary<dynamic> viewDataDictionary = new ViewDataDictionary<dynamic>();
viewDataDictionary.Add("path", "/news/search/");
}
<div class="container padding-bottom-40">
<div class="row">
<div class="bread-Body">
<ul class="breadcrumb">
<li>当前位置:<a href="@Html.SiteUrl()"><span>首页</span></a></li>
<li><a href="#">搜索</a></li>
</ul>
</div>
<div class="all-list">
<ul>
@{
int num = 0;
int num2 = 0;
foreach (var item in infoList)
{
num++;
num2++;
string url = "/"+ item.name+"/"+item.id+".cshtml";
string date = item.thedate.ToString("yyyy-MM-dd");
if (num2 % 8 == 0)
{
<li>
<a href="@url">@(item.title)</a>
<span>@date</span>
</li>
<hr />
}
else
{
<li>
<a href="@url">@(item.title)</a>
<span>@date</span>
</li>
}
}
}
</ul>
@if (num == 0)
{
<div class="line-height-2 padding-bottom-20 padding-top-20 font-size-16">
暂时没有找到匹配的记录,请更换一个关键词重新搜索!
</div>
}
@Html.Partial("PagebreakPartial", pageInfo, viewDataDictionary)
</div>
</div>
</div>