0

i wanna call C# action using Ajax, My question is how to redirect my action to the result page after the sucess of the function i tried :

  $.ajax({
        type: 'Get',
        url: "/OffreLocation/SearchOffer",
        data: {
            quartier: Quartier,
            superficieMin: SuperficieMin,
            superficieMax: SuperficieMax,
            budgetMin: BudgetMin,
            budgetMax: BudgetMax
              },           
        success: function (response) {
            window.location.href = "/Home/SearchResult";
        }

    });

but it returns the actual View !!

my action :

 [HttpGet]
    public ActionResult SearchOffer(int quartier, int superficieMin, int superficieMax, int budgetMin, int budgetMax)
    {


         List<OffreLocation> SearchedOffer = db.PublicationSet.OfType<OffreLocation>().Where( model => model.QuartierQuartier_Id == quartier && model.OffreLocation_Superficie > superficieMin && model.OffreLocation_Superficie < superficieMax).ToList();
         return RedirectToAction("SearchResult", "Home", SearchedOffer);

    }

any help !

404NotFound
  • 635
  • 1
  • 6
  • 14
  • 5
    If you want to redirect the browser, why use AJAX in the first place? When using AJAX the "redirect" needs to happen on the client, not on the server. It looks like you're doing both. Is the client not redirecting as expected? Is the `success` handler invoked at all? – David Jul 02 '14 at 13:12
  • so if the redirect need to happen in the client side, i just need to keep window.location.href = "/Home/SearchResult"; in the ajax method and use a void action insteed of ActionResult ! but how to pass the data returned from the action method in window.location.href = "/Home/SearchResult" ! – 404NotFound Jul 02 '14 at 13:50
  • 1
    Generally for an AJAX call when you need to return data to the client you'd return it as JSON (using `return Json(someObject)` in the controller). So if the redirect needs to include information in the query string then you can return that information as JSON and build the query string from it in the `success` handler. Though, again, if you're performing a redirect anyway then there isn't any need for AJAX here. Simply submitting the form to `SearchOffer` and redirecting from server-side code accomplishes the same thing with fewer steps and less complexity. – David Jul 02 '14 at 13:54

1 Answers1

0

I've read your comments too and come to a assumption that, you tried to use AJAX with result need to update with new records, First in a proper coding standard you can't use AJAX if you need a page refresh like you doing with return RedirectToAction("SearchResult", "Home", SearchedOffer); You have to use AJAX when you need not to reload the page but only a portion of a page needs refresh (EX: grid, dropdown, etc). Its very easy, try to re-write your code as like below

success: function(response) {
alert(response);
$("#SomeDiv").html(response);     
  }

Like above i have binded to a plain div element but you can bind the data wherever you want (JqGrid, DataTable, Div, etc.,), but before you must unload the previous data. If you using Datatable please check this link Data Table Refrsh with new data If JQgrid please look @ JqGrid Auto Refresh Or If you binding data in any other third party controll you have to look on you own to refresh that with new search data. Hope this helps.....

Community
  • 1
  • 1
Anto king
  • 1,222
  • 1
  • 13
  • 26