With MVC 4.0, RedirectToAction does not display PartialView as Partial, it sends the whole page

I have something a little complicated, but I don’t understand why this will not work: I have JSTree in my Index.cshtml. When node is selected, I make an Ajax call

public async Task<ActionResult> GetEBooksItems(string id) 

which return a partial view. This works EXCELLENT GOOD.

Now, in addition to searching for a tree, I have a form to add some criteria to narrow my search. I am using Ajax.BeginForm to send data back to the controller.

  public async Task<ActionResult> GetEBooksCustom(GenericSearchViewModel filter) { vm = filter; //Session["GenreNodeId"] = id; Session["SearchCondition"] = (string.IsNullOrEmpty(vm.Condition) != true) ? vm.Condition : "All"; Session["MaximumPrice"] = (string.IsNullOrEmpty(vm.MaxPrice) != true) ? vm.MaxPrice : "999999"; Session["MinimumPrice"] = (string.IsNullOrEmpty(vm.MinPrice) != true) ? vm.MinPrice : "0"; Session["SearchIndex"] = (string.IsNullOrEmpty(vm.SearchIndex) != true) ? vm.SearchIndex : "KindleStore"; Session["SortOrder"] = (string.IsNullOrEmpty(vm.SortOrder) != true) ? vm.SortOrder : "price"; Session["KeyWords"] = (string.IsNullOrEmpty(vm.Keywords) != true) ? vm.Keywords : ""; Session["Title"] = (string.IsNullOrEmpty(vm.Title) != true) ? vm.Title : ""; Session["Author"] = (string.IsNullOrEmpty(vm.Author) != true) ? vm.Author : ""; return RedirectToAction("GetEBooksItems", new { id = vm.CategoryNodeId }); } 

to this method, which in turn calls the first using RedirectToAction.

I try everything that I could think of, always get the results as a whole page, and not a partial representation, as in the first method.

It seems that calling GetEBooksCustom is not considered as an Ajax call. What am I missing? Thanks for your help, Bernard.

**** Edit after 6 hours **********

In between, I did what was suggested, that is, extract the functions from "GetEBooksItems" so that I could directly return a PartialView instead of using RedirectToAction. In the debugger it seems that everything is perfect, I see a 200 response from the server, BUT I still get a full pageview, and the url displays the controller method, while this should not be for ajax call.

The fact is that I basically have the same form somewhere else, where it works as expected. I can’t understand what could be different.

As always, thanks for your help and suggestions. Bernard

+4
source share
2 answers

I ended up using the jQuery Form plugin instead of Ajax.BeginForm. Having lost hours on this, it took me about 10 minutes to get it to work exactly as expected!

For those who may be interested, here are 2 types of coding one above the other:

  //using (Ajax.BeginForm("GetEBooksCustom", "Home", new AjaxOptions { // UpdateTargetId = "ListOfBooksHere", // HttpMethod = "POST", // OnBegin="ShowProcessingMsg", // OnComplete="HideProcessingMsg" $("#eBooksForm").ajaxForm({ url: "/home/GetEBooksCustom", type: "POST", success: function(data) { $("#ListOfBooksHere").html(data); } }); 

I was not worried about OnBegin and OnComplete as I use this:

  $(document).ajaxSend(function() { $('.loadingIndicator').show(); }); $(document).ajaxComplete(function() { $('.loadingIndicator').hide(); }); 
0
source

I would suggest that this happens because RedirectToAction returns 302 to your browser, instructing it to, literally, redirect to another action. Your browser handles this by redirecting the entire page. You should (depending on the content of the GetEBooksItems method, be able to get around this simply by calling another method directly, rather than using a redirect:

 return GetEBooksItems(vm.CategoryNodeId); 

Or, even better, abstract the logic from GetEBooksItems into a separate method, reuse it in GetEBooksCustom and return the PartialView directly. This way you do not have to bypass the redirect at all.

0
source

All Articles