views:

61

answers:

2

Hi all,

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?

+3  A: 

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;
         }
       );
s_hewitt
Hmm this seems like the ticket. 'result.url' doesn't work though. If I alert 'result', I get the view (so the DOCTYPE....etc stuff). How would I get the url from there?
Darcy
Why do an AJAX post if you're going to just turn around and do a normal GET by setting the url?
tvanfosson
I think in the way tvanfosson describes, inserting the HTML into a div. You'll probably want to extract the elements within the body tag only though.
s_hewitt
Sounds like you want the partial view. I'm not too familiar with MVC.
s_hewitt
I would like to do that. Is that built in to jquery or anything? A quick search on the net made it seem like the solution would be to a regex on it.
Darcy
I think you want a partial view, otherwise those regex solutions or some sort of parsing html as xml then loading the node is all i can think of.
s_hewitt
I'm going with the partial view solution. Thanks
Darcy
+2  A: 

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>
tvanfosson
Thanks tvan. Does it have to be a partial view? Why couldnt I return the entire view?
Darcy
@Darcy - that would basically be the same as doing a normal (non-AJAX) post. Using a partial view only sends down the updated parts, which is sort of the point of using AJAX.
tvanfosson