如何在不使用ajax mvc的情况下通过按钮单击调用控制器
一般来说,我对网络编程很陌生,所以这可能是一个非常简单的问题。然而我在网上找不到任何东西。
我想要做的是使用键入的搜索字符串调用我的控制器,并从数据库返回结果并对结果进行分页。现在我正在使用按下按钮时调用的函数。该函数如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想使用 AJAX,那么您需要将文本字段放置在页面上的表单元素中,如下所示:
然后在控制器中返回带有结果列表的视图。
您可能还想研究 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:
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.
我认为您实际上可能正在寻找 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.
Aspx:
控制器:
Aspx:
Controller: