【问题标题】:How to make a search for item and show the result in seperated page [ASP MVC]如何搜索项目并在单独的页面中显示结果 [ASP MVC]
【发布时间】:2016-02-15 20:08:41
【问题描述】:

我只想搜索我的网站,在我开始工作之前,我只想对我的工作步骤进行排序。

我的网站类型是书籍网站。这是我想知道的:

  1. 我的导航栏中有一个搜索框。
  2. 当有人输入书名进行搜索时,我想显示此结果 在此页面中搜索:

    Localhost/Search?string=C++
    
  3. 如何调用将从视图中处理搜索的方法 ?
  4. 控制器方法应该如何,还有视图页面。
  5. 如何对搜索页面进行分页?

我不需要代码,我只是希望有人帮我列出我应该做的步骤,按照我的要求进行搜索,非常感谢大家......

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 search razor


    【解决方案1】:

    您可以将用于搜索的输入框保留在表单中,并将表单的方法值保持为 GET。

    @using (Html.BeginForm("Search", "Students", FormMethod.Get, null))
    {
       <input type="text" name="searchTerm" />
       <input type="submit" />
    }
    

    现在您的StudentsController 中应该有一个名为Search 的操作方法,它接受名为searchTerm 的参数中的字符串值。

    public ActionResult Search(string searchTerm)
    {
      // use searchTerm variable to get data and pass to view
      return View();
    }
    

    现在在您的视图 (~/Views/Students/Search.cshtml) 中,您可以使用传递的数据来显示搜索结果。

    对于分页,您可以将另一个参数添加到您将用于获取特定数据子集的操作方法中。

    public ActionResult Search(string searchTerm,int page=1,size=10)
    {
       // use searchTerm variable to get data and pass to view
       // page number is in page variable 
       // size is in size variable
       return View();
    }
    

    现在您需要调整视图以显示所有页码并链接到相同的操作方法,并在 url 中传递页码

    /Students/Search?searchTerm=Java&page=2&size=25
    

    【讨论】:

    • 它工作了,但分页不起作用,也许我错过了什么,你能向我解释更多关于分页的信息吗?谢谢
    • 您基本上需要编写代码来将项目总数除以大小,并将其与您的数据(要显示)一起发送到视图并显示它。
    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    相关资源
    最近更新 更多