ASP.NET MVC 搜索
您好,我正在使用 ASP.NET MVC 构建一个非常简单的搜索系统。我最初是通过在控制器内执行所有逻辑来工作的,但现在我将代码逐段移动到存储库中。谁能帮我做到这一点。谢谢。
这是控制器中的原始操作结果。
public ActionResult Index(String query)
{
var ArticleQuery = from m in _db.ArticleSet
select m;
if (!string.IsNullOrEmpty(query))
{
ArticleQuery = ArticleQuery.Where(m => m.headline.Contains(query) orderby m.posted descending);
}
return View(ArticleQuery.ToList());
}
正如您所看到的,Index 方法既用于初始文章列表,也用于搜索结果(它们使用相同的视图)。
在新系统中,控制器中的代码如下:
public ActionResult Index()
{
return View(_repository.ListAll());
}
存储库具有以下代码:
public IList<Article> ListAll()
{
var ArticleQuery = (from m in _db.ArticleSet
orderby m.posted descending select m);
return ArticleQuery.ToList();
}
接口具有以下代码:
public interface INewsRepository
{
IList<Article> ListAll();
}
所以我现在需要做的是使用此将搜索查询内容添加到存储库/控制器方法中新方式。有人可以帮忙吗?谢谢。
编辑:INewsRespository 文件现在看起来:
namespace MySuperAwesomeApp.Models
{
public interface INewsRepository
{
IList<Article> ListAll();
//Article Details(int id);
//Article Create([Bind(Exclude = "Id")]Article articleToCreate);
}
}
Hi I'm building a very simple search system using ASP.NET MVC. I had it originally work by doing all the logic inside the controller but I am now moving the code piece by piece into a repository. Can anyone help me do this. Thanks.
Here is the original Action Result that was in the controller.
public ActionResult Index(String query)
{
var ArticleQuery = from m in _db.ArticleSet
select m;
if (!string.IsNullOrEmpty(query))
{
ArticleQuery = ArticleQuery.Where(m => m.headline.Contains(query) orderby m.posted descending);
}
return View(ArticleQuery.ToList());
}
As you can see, the Index method is used for both the initial list of articles and also for the search results (they use the same view).
In the new system the code in the controller is as follows:
public ActionResult Index()
{
return View(_repository.ListAll());
}
The Repository has the following code:
public IList<Article> ListAll()
{
var ArticleQuery = (from m in _db.ArticleSet
orderby m.posted descending select m);
return ArticleQuery.ToList();
}
and the Interface has the following code:
public interface INewsRepository
{
IList<Article> ListAll();
}
So what I need to do is now add in the search query stuff into the repository/controller methods using this new way. Can anyone help? Thanks.
EDIT: The INewsRespository file as it looks now:
namespace MySuperAwesomeApp.Models
{
public interface INewsRepository
{
IList<Article> ListAll();
//Article Details(int id);
//Article Create([Bind(Exclude = "Id")]Article articleToCreate);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的接口
INewsRepository
需要添加以下内容:Repository
然后在实现
INewsRepository
的NewsRepository
中:在控制器中,您处理以下情况:查询不包含信息:
控制器
Your interface
INewsRepository
needs to have the following added:Repository
Then in the
NewsRepository
that implementsINewsRepository
:In the controller, you handle the case where the query doesn't contain information:
Controller
查看我的代码示例来回答这个问题 - 希望他们能帮助你......
Look at my code samples in the answer to this question - hope they will help you...
乔治很接近,但他的代码无法编译(articleQuery 在返回时超出范围)
更新:
好的,您的代码最终应该如下所示:
更新 2(@Cameron url 问题)
将以下代码放入您的控制器中一会儿,看看您得到什么结果:
看看您得到什么。如果您没有看到某些消息,那么您必须路由到另一个控制器/操作。如果什么也没得到,请尝试 http://localhost:8888/home/index?query=henry
George was close, but his code won't compile (articleQuery is out of scope at the return)
UPDATE:
Ok, your code should ultimately look like this:
Update 2 (@Cameron url question)
Throw the following code into your controller for a moment and see what result you get:
And see what you get. If you don't see some message, then you must be routing to another controller/action. If you get nothing, try http://localhost:8888/home/index?query=henry
这应该可以解决问题:
并且您可能会从研究我当前的搜索实现中受益。但没有分页,它更像是过滤而不是搜索。它在底层使用 Linq to NHibernate。
Html:
控制器:
和存储库 + SearchCriteria 类:
This should do the trick:
And You might benefit from investigating my current search implementation. There is no paging though and it's more like filtering instead of searching. It uses Linq to NHibernate underneath.
Html:
Controller:
And repository +
SearchCriteria
class: