jquery $.Post 不会改变视图。我该怎么办?

发布于 2024-08-22 19:46:03 字数 671 浏览 4 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

孤芳又自赏 2024-08-29 19:46:03

我不太熟悉 MVC,但是除了将用户重定向到 window.location 中的 URL 之外,您的 ActionResult 还能做什么?

需要实现一个回调,来自 jQuery 文档

 jQuery.post( url, data, callback, type )

您可能 传递 URL 和数据,但不传递任何关于要如何处理返回数据的信息。

 $.post( "MyController/SearchResults/", 
         {searchString: searchBox.value,page: 0},
         function(result){
                //do something with what your actionresult returns here
                window.location = result.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:

 jQuery.post( url, data, callback, type )

So you're passing the URL and the data, but nothing about what you want to do with the returned data.

 $.post( "MyController/SearchResults/", 
         {searchString: searchBox.value,page: 0},
         function(result){
                //do something with what your actionresult returns here
                window.location = result.url;
         }
       );
梦罢 2024-08-29 19:46:03

设置 SearchResults 操作以返回仅包含执行搜索时要更新的页面部分的部分视图。该部件应包含在某个容器中,以便于更换。然后使用 post 方法上的回调机制将该 DIV 的内容替换为从 SearchResults 操作返回的部分视图结果。从处理程序返回 false 以停止执行默认提交操作。

function SubmitSearch() { 

    var searchBox = $('#searchBox'); 

    $.post("MyController/SearchResults/", 
    { 
        searchString: searchBox.val(), 
        page: 0 
    }, function(data) {
        $('#searchResults').html( data );
    }); 

    return false;
}

这假设一些视图代码类似于:

 <% using (Html.BeginForm()) { %
      <label for="searchBox">Search:</label>
      <%= Html.TextBox("searchBox") %>
 <% } %>
 <div id="searchResults">
 </div>

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.

function SubmitSearch() { 

    var searchBox = $('#searchBox'); 

    $.post("MyController/SearchResults/", 
    { 
        searchString: searchBox.val(), 
        page: 0 
    }, function(data) {
        $('#searchResults').html( data );
    }); 

    return false;
}

This assumes some view code that looks something like:

 <% using (Html.BeginForm()) { %
      <label for="searchBox">Search:</label>
      <%= Html.TextBox("searchBox") %>
 <% } %>
 <div id="searchResults">
 </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文