如何在不使用ajax mvc的情况下通过按钮单击调用控制器

发布于 2024-08-23 20:10:51 字数 774 浏览 4 评论 0原文

一般来说,我对网络编程很陌生,所以这可能是一个非常简单的问题。然而我在网上找不到任何东西。

我想要做的是使用键入的搜索字符串调用我的控制器,并从数据库返回结果并对结果进行分页。现在我正在使用按下按钮时调用的函数。该函数如下所示:

function SubmitSearch() {

    var searchBox = document.getElementById('searchBox');
    var searchButton = document.getElementById('SearchButton');

    $.post("MyController/MyAction/",
    {
        searchString: searchBox.value,
        page: null
    }, function(result) {
        $('#searchResults').html(result);
        searchButton.value = "Search";
    });
}

发生的情况是我的控制器被调用,我的 searchResults div 填充了结果并分页。用户可以点击任何返回的搜索结果来查看详细信息。

问题是当用户单击浏览器的“后退”按钮时,页面会返回到输入搜索之前的状态,这是由于 ajax 调用造成的。我想做的是,调用控制器并像谷歌一样加载页面。我不会使用 PartialView,而是使用 View(我的猜测)。

我将如何调用控制器并让页面重新加载结果。我一定错过了一些基本的东西,因为这看起来绝对应该很容易。

I'm new to web programming in general so this is probably a really simple question. I couldn't find anything on the web however.

What I want to do is call my controller with the typed search string and return the results from a database and paginate the results. Right now I am using a function that is called when the button is pressed. The function looks like so:

function SubmitSearch() {

    var searchBox = document.getElementById('searchBox');
    var searchButton = document.getElementById('SearchButton');

    $.post("MyController/MyAction/",
    {
        searchString: searchBox.value,
        page: null
    }, function(result) {
        $('#searchResults').html(result);
        searchButton.value = "Search";
    });
}

What happens is my controller is called, and my searchResults div is populated with the results and paginated. The user can click any search result returned to view the details.

The problem is when the user clicks the browsers 'back' button, the page returns to the state before the search was entered, and this is because of the ajax call. What I want to do is, call the controller and have the page load like google would. Instead of using a PartialView, I would use a View (my guess).

How would I call the controller and have the page RELOAD with the results. I must be missing something fundamental because this definitely seems like it should be easy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

盛夏尉蓝 2024-08-30 20:10:51

如果您不想使用 AJAX,那么您需要将文本字段放置在页面上的表单元素中,如下所示:

<form action="MyController/MyAction/" method="get">
  <input id="SearchBox" name="SearchBox" type="text" />
  <button type="submit">Search</button>
</form>

然后在控制器中返回带有结果列表的视图。

您可能还想研究 RESTful URL 和 PRG(发布、重定向、获取) 模式,用于维护后退按钮的完整性并启用正确的页面书签等。

If you don't want to use AJAX then you need to place your text field in a form element on your page, something like:

<form action="MyController/MyAction/" method="get">
  <input id="SearchBox" name="SearchBox" type="text" />
  <button type="submit">Search</button>
</form>

Then in your controller return the view with the list of results.

You probably also want to look into RESTful URLs and the PRG (Post, Redirect, Get) pattern to maintain the integrity of the back button and enable correct bookmarking of pages etc.

谁把谁当真 2024-08-30 20:10:51

我认为您实际上可能正在寻找 AJAX 历史库来在按下“后退”按钮时提供帮助,而不是更改您的应用程序。请查看这篇博客文章

I think you might actually be looking for an AJAX History library to help when the Back button is pressed rather than altering your app. Take a look at this blog post.

慵挽 2024-08-30 20:10:51

Aspx:

<% using (Html.BeginForm<MyController>(m => m.MyAction(null)) { %> 
    <%= Html.TextBox("q"); %>
<% } %>
// Listing

控制器:

public class MyController : Controller
{
    public ActionResult MyAction(string q)
    {
        var repository; // instance of your repository.

        if (String.IsNullOrEmpty(q))
        {
            return View(repository.GetAllBlogs());
        }
        return View(repository.SearchBlogs(q));
    }
}

Aspx:

<% using (Html.BeginForm<MyController>(m => m.MyAction(null)) { %> 
    <%= Html.TextBox("q"); %>
<% } %>
// Listing

Controller:

public class MyController : Controller
{
    public ActionResult MyAction(string q)
    {
        var repository; // instance of your repository.

        if (String.IsNullOrEmpty(q))
        {
            return View(repository.GetAllBlogs());
        }
        return View(repository.SearchBlogs(q));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文