ASP.NET MVC 搜索

发布于 2024-10-10 07:49:52 字数 1456 浏览 2 评论 0原文

您好,我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫南 2024-10-17 07:49:52

您的接口 INewsRepository 需要添加以下内容:

IList<Article> Search(string query);

Repository

然后在实现 INewsRepositoryNewsRepository 中:

public IList<Article> INewsRespository.Search(string query)
{
    var articleQuery = (from m in _db.ArticleSet
                        where m.headline.Contains(query)
                        orderby m.posted descending select m).ToList();
}

在控制器中,您处理以下情况:查询不包含信息:

控制器

//just for this example. You should look at DI/IoC for Real World Usage.
INewsRepository repository; 
public ActionResult ListArticles(string query)
{
    repository = new NewsRepository();
    List<Article> articles = new List<Article>();
    if (string.IsNullOrEmpty(query))
    {
        articles = repository.ListAll();
    }
    else 
    { 
        articles = repository.Search(query);
    }
    return View(articles);
}

Your interface INewsRepository needs to have the following added:

IList<Article> Search(string query);

Repository

Then in the NewsRepository that implements INewsRepository:

public IList<Article> INewsRespository.Search(string query)
{
    var articleQuery = (from m in _db.ArticleSet
                        where m.headline.Contains(query)
                        orderby m.posted descending select m).ToList();
}

In the controller, you handle the case where the query doesn't contain information:

Controller

//just for this example. You should look at DI/IoC for Real World Usage.
INewsRepository repository; 
public ActionResult ListArticles(string query)
{
    repository = new NewsRepository();
    List<Article> articles = new List<Article>();
    if (string.IsNullOrEmpty(query))
    {
        articles = repository.ListAll();
    }
    else 
    { 
        articles = repository.Search(query);
    }
    return View(articles);
}
生生漫 2024-10-17 07:49:52

查看我的代码示例来回答这个问题 - 希望他们能帮助你......

Look at my code samples in the answer to this question - hope they will help you...

十年九夏 2024-10-17 07:49:52

乔治很接近,但他的代码无法编译(articleQuery 在返回时超出范围)

// public IList<Article> IRespository.Search(string query)
public IList<Article> Search(string query)
{
    var articleQuery = (from m in _db.ArticleSet orderby m.posted descending select m);
    return (string.IsNullOrEmpty(query))
        ? articleQuery.ToList()  // I'm not 100% sure if ToList is required
        : articleQuery.Where(m => m.headline.Contains(query)).ToList();
}

更新:
好的,您的代码最终应该如下所示:

public interface INewsRepository
{
    IList<Article> Search(string query);
    IList<Article> ListAll();  // You need this too, right?
}

public class NewsRepository : INewsRepository
{
    public IList<Article> Search(string query)
    {
        // Code as above.
    }

    public IList<Article> ListAll()
    {
        // Code as in your question.
    }
}

更新 2(@Cameron url 问题)

将以下代码放入您的控制器中一会儿,看看您得到什么结果:

public ActionResult Index(string query)
{
    Response.Write("The Query string was :" + query);
    return new EmptyResult();
}

看看您得到什么。如果您没有看到某些消息,那么您必须路由到另一个控制器/操作。如果什么也没得到,请尝试 http://localhost:8888/home/index?query=henry

George was close, but his code won't compile (articleQuery is out of scope at the return)

// public IList<Article> IRespository.Search(string query)
public IList<Article> Search(string query)
{
    var articleQuery = (from m in _db.ArticleSet orderby m.posted descending select m);
    return (string.IsNullOrEmpty(query))
        ? articleQuery.ToList()  // I'm not 100% sure if ToList is required
        : articleQuery.Where(m => m.headline.Contains(query)).ToList();
}

UPDATE:
Ok, your code should ultimately look like this:

public interface INewsRepository
{
    IList<Article> Search(string query);
    IList<Article> ListAll();  // You need this too, right?
}

public class NewsRepository : INewsRepository
{
    public IList<Article> Search(string query)
    {
        // Code as above.
    }

    public IList<Article> ListAll()
    {
        // Code as in your question.
    }
}

Update 2 (@Cameron url question)

Throw the following code into your controller for a moment and see what result you get:

public ActionResult Index(string query)
{
    Response.Write("The Query string was :" + query);
    return new EmptyResult();
}

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

冷清清 2024-10-17 07:49:52

这应该可以解决问题:

public IList<Article> SearchByTitle(string query){
    var q=_db.ArticleSet;
    if (!string.IsNullOrEmpty(query))    
        q=q.Where(a=>a.Title.Contains(query));
    return q.OrderBy(x=>x.Title).ToList();
}

并且您可能会从研究我当前的搜索实现中受益。但没有分页,它更像是过滤而不是搜索。它在底层使用 Linq to NHibernate。

Html:

@model SearchCriteria
@using (Html.BeginForm("Search","Applications",FormMethod.Post)){
<span>@Html.ShowLink("Search applications...","searchbox")</span>
<fieldset class="searchapplications">
 <legend>@Html.HideLink("Search applications:","searchbox",container:"fieldset")</legend>
 <table class="alignright" style="width:100%;">
  <tr>
   @Html.TDInputFor(x=>x.ProjectIndex)
   @Html.TDInputFor(x=>x.OpeningStatus)
   <td>
   Duration from @Html.EditorFor(x=>x.DurationFrom)
   to @Html.EditorFor(x=>x.DurationTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Acronym)
   @Html.TDInputFor(x=>x.AdmStatus)
   <td>
   Requested grant from @Html.EditorFor(x=>x.RequestedGrantFrom)
   to @Html.EditorFor(x=>x.RequestedGrantTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Priority)
   @Html.TDInputFor(x=>x.EligStatus)
   <td>
   Score from @Html.EditorFor(x=>x.QualScoreFrom)
   to @Html.EditorFor(x=>x.QualScoreTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Applicant)
   @Html.TDInputFor(x=>x.QualStatus)
 </tr>
 <tr>
   <td colspan="2"></td>
   @Html.TDInputFor(x=>x.JMCDecision)
   <td colspan="2"></td>
 </tr>
 </table>
 @Html.HiddenFor(x=>x.SortBy)
 @Html.HiddenFor(x=>x.SortAsc)
 <br />
 <br />
 <input type="submit" value="Search" />
</fieldset>
}

控制器:

public class ApplicationsController{
 [HttpPost]
 public ActionResult Search(SearchCriteria input){
   if(input==null) input=Session[Critkey] as SearchCriteria??new SearchCriteria();
   Session[Critkey]=input;
   var applications=_applications.Search(input);
   var mapped=this.MapList<Application,ApplicationModel>(applications);
   return View(new SearchModel {Applications=mapped,SearchCriteria=input});
 }
}

和存储库 + SearchCriteria 类:

  public class AllApplications:IAllApplications{    
    public IEnumerable<Application> Search(SearchCriteria inp){
      var c=_session.Query<Application>();
      c=ApplicationSearchEagerLoading.Attach(c);
      var q=c.AsQueryable();
      q=AddSearchFilters(inp,q);
      if(!string.IsNullOrEmpty(inp.SortBy)){
        var s=SearchCriteria.SortByTranslations[inp.SortBy];
        q=(inp.SortAsc?q.OrderBy(s):q.OrderByDescending(s)).AsQueryable();
      }
      return q.AsEnumerable();
    }      
    private static IQueryable<Application> AddSearchFilters
       (SearchCriteria inp,IQueryable<Application> q){
      if(!string.IsNullOrEmpty(inp.Acronym))
        q=q.Where(a=>a.Project.Acronym.Contains(inp.Acronym));
      if(!string.IsNullOrEmpty(inp.Applicant))
        q=q.Where(a=>a.Project.Applicant.Name.Contains(inp.Applicant));
      if(!string.IsNullOrEmpty(inp.Priority))
        q=q.Where(a=>a.Project.Priority.Name.Contains(inp.Priority));
      if(!string.IsNullOrEmpty(inp.ProjectIndex))
        q=q.Where(a=>a.Project.ProjectIndex.Contains(inp.ProjectIndex));
      if(inp.OpeningStatus!=null)
        q=q.Where(a=>a.OpeningStatus==inp.OpeningStatus);
      if(inp.AdmStatus!=null)
        q=q.Where(a=>a.AdmCheck.Status==inp.AdmStatus);
      if(inp.EligStatus!=null)
        q=q.Where(a=>a.EligCheck.Status==inp.EligStatus);
      if(inp.QualStatus!=null)
        q=q.Where(a=>a.QualAssessmentStatus==inp.QualStatus);
      if(inp.JMCDecision!=null)
        q=q.Where(a=>a.JMCDecision==inp.JMCDecision);
      if(!string.IsNullOrEmpty(inp.DurationFrom)
         &&!string.IsNullOrEmpty(inp.DurationTo))                   
      q=q.Where(a=>a.Project.Description
                .DurationInMonths>=inp.DurationFrom.ExtractNumber()
             &&a.Project.Description
                .DurationInMonths<=inp.DurationTo.ExtractNumber());
      else{
        if(!string.IsNullOrEmpty(inp.DurationFrom))
          q=q.Where(a=>a.Project.Description
             .DurationInMonths>=inp.DurationFrom.ExtractNumber());
        if(!string.IsNullOrEmpty(inp.DurationTo))               
          q=q.Where(a=>a.Project.Description
            .DurationInMonths<=inp.DurationTo.ExtractNumber());
      }              
      //...
      return q;
    }
  }

  public class SearchCriteria{
    internal static Dictionary<string,Func<Application,object>>
    SortByTranslations=new Dictionary<string,Func<Application,object>>
                         {
                           {"ProjectIndex",x=>x.Project.IndexNr},
                           {"Acronym",x=>x.Project.Acronym},
                           {"Applicant",x=>x.Project.Applicant.Name},
                           {"Priority",x=>x.Project.Priority.Name},
                           {"OpeningStatus",x=>x.OpeningStatus},
                           {"AdmStatus",x=>x.AdmCheck.Status},
                           {"EligStatus",x=>x.EligCheck.Status},
                           {"QualStatus",x=>x.QualAssessmentStatus},
                           {"JMCDecision",x=>x.JMCDecision},
                         };
    public string RequestedGrantTo{get;set;}
    public string RequestedGrantFrom{get;set;}
    public string DurationFrom{get;set;}
    public string DurationTo{get;set;}
    public string QualScoreFrom{get;set;}
    public string QualScoreTo{get;set;}
    public QualAssessmentStatus QualStatus{get;set;}
    public EligCheckStatus EligStatus{get;set;}
    public AdmCheckStatus AdmStatus{get;set;}
    public OpeningStatus OpeningStatus{get;set;}
    public JMCDecision JMCDecision{get;set;}
    public string Applicant{get;set;}
    public string Priority{get;set;}
    public string Acronym{get;set;}
    public string ProjectIndex{get;set;}
    public string SortBy{get;set;}
    public bool SortAsc{get;set;}
}

This should do the trick:

public IList<Article> SearchByTitle(string query){
    var q=_db.ArticleSet;
    if (!string.IsNullOrEmpty(query))    
        q=q.Where(a=>a.Title.Contains(query));
    return q.OrderBy(x=>x.Title).ToList();
}

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:

@model SearchCriteria
@using (Html.BeginForm("Search","Applications",FormMethod.Post)){
<span>@Html.ShowLink("Search applications...","searchbox")</span>
<fieldset class="searchapplications">
 <legend>@Html.HideLink("Search applications:","searchbox",container:"fieldset")</legend>
 <table class="alignright" style="width:100%;">
  <tr>
   @Html.TDInputFor(x=>x.ProjectIndex)
   @Html.TDInputFor(x=>x.OpeningStatus)
   <td>
   Duration from @Html.EditorFor(x=>x.DurationFrom)
   to @Html.EditorFor(x=>x.DurationTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Acronym)
   @Html.TDInputFor(x=>x.AdmStatus)
   <td>
   Requested grant from @Html.EditorFor(x=>x.RequestedGrantFrom)
   to @Html.EditorFor(x=>x.RequestedGrantTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Priority)
   @Html.TDInputFor(x=>x.EligStatus)
   <td>
   Score from @Html.EditorFor(x=>x.QualScoreFrom)
   to @Html.EditorFor(x=>x.QualScoreTo)
   </td>
  </tr>
  <tr>
   @Html.TDInputFor(x=>x.Applicant)
   @Html.TDInputFor(x=>x.QualStatus)
 </tr>
 <tr>
   <td colspan="2"></td>
   @Html.TDInputFor(x=>x.JMCDecision)
   <td colspan="2"></td>
 </tr>
 </table>
 @Html.HiddenFor(x=>x.SortBy)
 @Html.HiddenFor(x=>x.SortAsc)
 <br />
 <br />
 <input type="submit" value="Search" />
</fieldset>
}

Controller:

public class ApplicationsController{
 [HttpPost]
 public ActionResult Search(SearchCriteria input){
   if(input==null) input=Session[Critkey] as SearchCriteria??new SearchCriteria();
   Session[Critkey]=input;
   var applications=_applications.Search(input);
   var mapped=this.MapList<Application,ApplicationModel>(applications);
   return View(new SearchModel {Applications=mapped,SearchCriteria=input});
 }
}

And repository + SearchCriteria class:

  public class AllApplications:IAllApplications{    
    public IEnumerable<Application> Search(SearchCriteria inp){
      var c=_session.Query<Application>();
      c=ApplicationSearchEagerLoading.Attach(c);
      var q=c.AsQueryable();
      q=AddSearchFilters(inp,q);
      if(!string.IsNullOrEmpty(inp.SortBy)){
        var s=SearchCriteria.SortByTranslations[inp.SortBy];
        q=(inp.SortAsc?q.OrderBy(s):q.OrderByDescending(s)).AsQueryable();
      }
      return q.AsEnumerable();
    }      
    private static IQueryable<Application> AddSearchFilters
       (SearchCriteria inp,IQueryable<Application> q){
      if(!string.IsNullOrEmpty(inp.Acronym))
        q=q.Where(a=>a.Project.Acronym.Contains(inp.Acronym));
      if(!string.IsNullOrEmpty(inp.Applicant))
        q=q.Where(a=>a.Project.Applicant.Name.Contains(inp.Applicant));
      if(!string.IsNullOrEmpty(inp.Priority))
        q=q.Where(a=>a.Project.Priority.Name.Contains(inp.Priority));
      if(!string.IsNullOrEmpty(inp.ProjectIndex))
        q=q.Where(a=>a.Project.ProjectIndex.Contains(inp.ProjectIndex));
      if(inp.OpeningStatus!=null)
        q=q.Where(a=>a.OpeningStatus==inp.OpeningStatus);
      if(inp.AdmStatus!=null)
        q=q.Where(a=>a.AdmCheck.Status==inp.AdmStatus);
      if(inp.EligStatus!=null)
        q=q.Where(a=>a.EligCheck.Status==inp.EligStatus);
      if(inp.QualStatus!=null)
        q=q.Where(a=>a.QualAssessmentStatus==inp.QualStatus);
      if(inp.JMCDecision!=null)
        q=q.Where(a=>a.JMCDecision==inp.JMCDecision);
      if(!string.IsNullOrEmpty(inp.DurationFrom)
         &&!string.IsNullOrEmpty(inp.DurationTo))                   
      q=q.Where(a=>a.Project.Description
                .DurationInMonths>=inp.DurationFrom.ExtractNumber()
             &&a.Project.Description
                .DurationInMonths<=inp.DurationTo.ExtractNumber());
      else{
        if(!string.IsNullOrEmpty(inp.DurationFrom))
          q=q.Where(a=>a.Project.Description
             .DurationInMonths>=inp.DurationFrom.ExtractNumber());
        if(!string.IsNullOrEmpty(inp.DurationTo))               
          q=q.Where(a=>a.Project.Description
            .DurationInMonths<=inp.DurationTo.ExtractNumber());
      }              
      //...
      return q;
    }
  }

  public class SearchCriteria{
    internal static Dictionary<string,Func<Application,object>>
    SortByTranslations=new Dictionary<string,Func<Application,object>>
                         {
                           {"ProjectIndex",x=>x.Project.IndexNr},
                           {"Acronym",x=>x.Project.Acronym},
                           {"Applicant",x=>x.Project.Applicant.Name},
                           {"Priority",x=>x.Project.Priority.Name},
                           {"OpeningStatus",x=>x.OpeningStatus},
                           {"AdmStatus",x=>x.AdmCheck.Status},
                           {"EligStatus",x=>x.EligCheck.Status},
                           {"QualStatus",x=>x.QualAssessmentStatus},
                           {"JMCDecision",x=>x.JMCDecision},
                         };
    public string RequestedGrantTo{get;set;}
    public string RequestedGrantFrom{get;set;}
    public string DurationFrom{get;set;}
    public string DurationTo{get;set;}
    public string QualScoreFrom{get;set;}
    public string QualScoreTo{get;set;}
    public QualAssessmentStatus QualStatus{get;set;}
    public EligCheckStatus EligStatus{get;set;}
    public AdmCheckStatus AdmStatus{get;set;}
    public OpeningStatus OpeningStatus{get;set;}
    public JMCDecision JMCDecision{get;set;}
    public string Applicant{get;set;}
    public string Priority{get;set;}
    public string Acronym{get;set;}
    public string ProjectIndex{get;set;}
    public string SortBy{get;set;}
    public bool SortAsc{get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文