jquery $.Post 不会改变视图。我该怎么办?
我对 MVC 还很陌生,所以这似乎是一个明显的问题,但是当发布到我的 ActionResult 时,ActionResult 会被调用,但视图不会改变。
我正在做的是有一个基本的搜索页面,用户可以在其中搜索我的数据库,然后返回结果并分页。
这是在我的控制器中调用 ActionResult 的 javascript。
function SubmitSearch() {
var searchBox = document.getElementById('searchBox');
$.post("MyController/SearchResults/",
{
searchString: searchBox.value,
page: 0
});
window.location = "../DriverStudio/Drivers/SearchResults/" + searchBox.value + '/0';
}
我当前的解决方案(这是一个可怕的黑客)是注释掉 $.post (因为我的路线是以 window.location 将调用 SearchResutls ActionResult 的方式设置的)并将 window.location 设置为用户搜索的内容for,从第 0 页开始。
必须有更好的方法。我应该怎么办?
I'm pretty new to MVC so this may seem like an obvious question, but when posting to my ActionResult, the ActionResult gets called, but the View doesn't change.
What I'm doing is having a basic search page where a user can search my database and the results are returned and paginated.
Here is javascript that calls that ActionResult in my controller.
function SubmitSearch() {
var searchBox = document.getElementById('searchBox');
$.post("MyController/SearchResults/",
{
searchString: searchBox.value,
page: 0
});
window.location = "../DriverStudio/Drivers/SearchResults/" + searchBox.value + '/0';
}
My current solution (which is a horrible hack) is to comment out the $.post(since my route is set up in a way where the window.location will call the SearchResutls ActionResult) and set the window.location to what the user searched for, starting at page 0.
There has to be a better way. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太熟悉 MVC,但是除了将用户重定向到 window.location 中的 URL 之外,您的 ActionResult 还能做什么?
需要实现一个回调,来自 jQuery 文档:
您可能 传递 URL 和数据,但不传递任何关于要如何处理返回数据的信息。
I'm not to familiar with MVC, but what does your ActionResult do other than redirect the user to that URL you have in window.location?
You probably need to implement a callback, from jQuery docs:
So you're passing the URL and the data, but nothing about what you want to do with the returned data.
设置 SearchResults 操作以返回仅包含执行搜索时要更新的页面部分的部分视图。该部件应包含在某个容器中,以便于更换。然后使用 post 方法上的回调机制将该 DIV 的内容替换为从 SearchResults 操作返回的部分视图结果。从处理程序返回 false 以停止执行默认提交操作。
这假设一些视图代码类似于:
Set up your SearchResults action to return a partial view containing only the part of the page to be updated when the search is performed. This piece should be contained in some container, making it easy to replace. Then use the callback mechanism on the post method to replace the contents of that DIV with the partial view result returned from your SearchResults action. Return false from your handler to stop the default submit action from being taken.
This assumes some view code that looks something like: