我为自己的网站做了自己的搜索引擎。我设法获得了一些结果。但我只能从sql表中获取数据。
现在,HTML表单如下所示:
<form action="@Url.Action("SearchResult","Home")" method="get" class="search-form">
<input id="searchNm" type="search" placeholder="Search..." class="search-field" name="SearchText" value="">
<button type="submit" class="search-submit">
<span class="search-icon">Search</span>
</button>
控制器:
LawyerDB context = new LawyerDB();
public ActionResult SearchResult(string SearchText)
{
return View(context.Articles.Where(x=>x.ArticleContent.Contains(SearchText) ||x.Headline.Contains(SearchText) || SearchText==null).ToList());
}
搜索结果:
@model IEnumerable<Law.Data.Model.Category>
@foreach(var item in Model){
<article class="col-sm-12">
<a href="@Url.Action("BlogDetails","Home",new { id=item.ArticleId})">
<img class="img-responsive" src="@item.ArticlePhoto">
</a>
<div class="blockContent">
<div class="blockTop">
<a href="@Url.Action("BlogDetails","Home",new { id=item.ArticleId})" class="layout-tittle" style="width:80%;" >@item.Headline</a>
</div>
<p class=" text-muted"><em><i class="glyphicon glyphicon-time fa-fw"></i>@String.Format("{0:MMMM dd, yyyy}", item.CreateDate) <span class="col-sm-offset-1"><i class="fa fa-comments-o" aria-hidden="true"></i>@item.Comments.Count()</span></em></p>
<hr>
@if (item.ArticleContent.Length > 150)
{
<p>
<em>
@Html.Raw(item.ArticleContent.Substring(0, 150))
</em>
</p>
}
else
{
<p>
<em>
@Html.Raw(item.ArticleContent)...
</em>
</p>
}
<div class="clearfix"></div>
<a class="read-more" href="@Url.Action("BlogDetails","Home",new { id=item.ArticleId})">Read More<i class="fa fa-long-arrow-right" aria-hidden="true"></i></a>
<hr>
</div>
}
我现在只能为文章表执行此操作。对于所有sql表,有一个很好的方法吗?
我的sql表:
public virtual DbSet<Article> Articles { get; set; }
public virtual DbSet<Authority> Authorities { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Comment> Comments { get; set;}
public virtual DbSet<Employee> Employees { get; set; }
public virtual DbSet<Label> Labels { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<SiteContent> SiteContents { get; set; }
答案 0 :(得分:0)
如果您使用的是SQL Server,我建议您查看全文索引功能。它将比简单的.Contains
子句做更多的事情,这只会显示完全匹配。您需要做更多的工作来创建存储过程。我不建议尝试从原始用户输入构建SQL语句,除非您期望Little Bobby Tables可以使用。