【问题标题】:How do I create a simple search box with a submit button to bring back a result set in MVC?如何创建一个带有提交按钮的简单搜索框以在 MVC 中返回结果集?
【发布时间】:2013-10-18 21:10:56
【问题描述】:

我对 MVC 很陌生,只是学习基础知识。我一直在关注 Nerd Dinner 并使用该演示来创建我自己的应用程序。我创建了一个页面,列出了一些含有卡路里、脂肪、蛋白质等的食物... (http://rjsfitness.net/CalorieList) 这是我自己的个人网站之一,我设置它来测试 MVC。我做了很多工作,但我被困在带有搜索按钮的文本框上。

我的查看页面有这个搜索代码:

    <form action="/CalorieList/Search" method="post" id="searchForm">
  <input type="text" name="searchTerm" id="searchTerm" value="" size="10" maxlength ="30" />
  <input type ="submit" value="Search" />
</form>

我的 global.asax 有这个路由代码:

        routes.MapRoute(
            "Search", // Route name
            "CalorieList/Search/{searchTerm}", // URL with parameters
            new { controller = "CalorieList", action = "Search", search = "" } // Parameter defaults
        );

我的控制器有这个代码:

        public ActionResult Index(int? page)
    {
        const int pageSize = 10;

        //load a list with the calorie list
        var calorieLists = calorieListRepository.GetAllCalorieLists();

        //var paginatedCalorieLists = calorieLists.Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
        var paginatedCalorieLists = new PaginatedList<CalorieList>(calorieLists, page ?? 0, pageSize);

        return View("Index", paginatedCalorieLists);
    }

    public ActionResult Search(String searchTerm)
    {
        const int pageSize = 100;
        int? page = 0;

        var calorieLists = calorieListRepository.GetCalorieListsBySearch(searchTerm);

        var paginatedCalorieLists = new PaginatedList<CalorieList>(calorieLists, page ?? 0, pageSize);

        return View("Index", paginatedCalorieLists);
    }

        return View("Index", paginatedCalorieLists);
    }

当我输入一个值并单击按钮时,会触发 Index 方法而不是控制器中的 Seach 方法,然后我会再次获得完整列表。如果我手动输入网址 (http://rjsfitness.net/CalorieList/Search/choc),我会得到正确的列表。

为什么我的按钮没有使用正确的路由点击并给我搜索结果?

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc


    【解决方案1】:

    你的表单实际上指向这个:

    <form id="form1" action="CalorieList" method="post">
    

    如果您使用 Html.BeginForm,则您传入了错误的参数。

    <% using (Html.BeginForm("Search", "CalorieList")) %>
    <% { %>
    // Your form fields
    <% } %>
    

    顺便说一句,为什么您的页面上有 ViewState?

    【讨论】:

    • 所以我应该将操作指向 CalorieList/Search 以使其工作。而且我不知道为什么页面上有 ViewState 但很好。我得想办法解决这个问题。
    • 奇怪的是,在查看页面上的实际代码是
      但查看源显示
      与 viewstate 以及上面的代码。我不知道视图状态是从哪里来的。
    • 它实际上是一个指向控制器操作的网络表单吗?
    • 发现 ViewState 问题。我在主窗体上有一个 runat=server 。我也发现了搜索的问题。搜索表单位于主表单内。我把它移了出来,它工作了,但我更喜欢你的解决方案。我将您的使用代码放在页面上,生成的 html 与我的表单代码相同。但它看起来更清洁你的方式。感谢您的帮助。
    【解决方案2】:

    因为我认为您要回到 Index ActionResult。在您的索引操作中放置一个断点并查看。

    您可能想要返回到路线而不是视图。

    编辑

    或者有一个搜索视图可以返回。

    或者你可以做一个 jQuery 回传并返回一个 PartialView 并用返回的部分视图 html 替换 div 的内容。

    如果这是您想要发表的评论,我可以提供代码和链接。

    编辑 2

    我在这个问题中稍微解释一下。如果您需要更多代码或示例,请添加另一条评论,我会发布没有问题。

    another approach to returning some thing to browser in mvc with ajax call instead of using response.write

    【讨论】:

    • 是的,我肯定会回到索引。我确实放了一个断点,它就是这样做的。我想我在上面解释过,但它可能在所有文本中都丢失了。是的,你解释的正是我想要的,我已经看到了一些你解释的不同方法的例子,但我只是对这个很陌生,一些代码示例会很棒。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多