0

I"m not sure if using Ajax.Beginform is even the right method for this, I have tried a few different things at this point.

I have a textbox and a button that users can use to search a database with. They are located on my _layout page. I originally had this on my index page and was using ajax.begin form to render a partial view. I want to display URL's to the user so they can navigate properly in the browser, so I have moved the search section to my _layouts page and I want to hook it up so that if they are on the index page, it does a postback to a search page. If they are on the search page then it would render the partial view.

I am stuck trying to get it to just navigate to a new page. I've tried using the Ajax.begin form (Code below) and adding code to the button directly (also below) but have not gotten it to redirect. With the Ajax I see a response come back in the Network tab of the browser debug window, but it doesn't render on the page.

Here's the code:

Ajax Attempt:

@using (Ajax.BeginForm("Search", "Home", new AjaxOptions { HttpMethod = "GET" })) {

            <div class="form-group" style="width:85%;">
                <div class="right-inner-addon">
                    <i class=" glyphicon glyphicon-search"></i>
                    <input type="text" data-autocomplete="@Url.Action("Quicksearch","Home")" class="form-control" placeholder="Search" name="q" value="@ViewBag.CurrenetFilter" />
                </div>
            </div>
            <div class="form-group">
                <button class="btn btn-default form-inline" type="submit">Search</button>
            </div>
        }
        <br />

    </div>

</div>
@RenderBody()

This is on the home controller:

   public ActionResult Search(string q, int? page,  string catb = null)
    {
       return RedirectToAction("Search", "Items", new { query = q, pageNum = page, categoryB = catb });
    }

This is on the Items controller:

 public ActionResult Search(string query, int? pageNum, string categoryB = null)
    {


        var pageNumber = pageNum ?? 1; // if no page was specified in the querystring, default to the first page (1)

        var items = db.items_with_descriptions
           .Where(r => r.category_c.Contains(query) && r.category_b.Contains(categoryB) || string.IsNullOrEmpty(query) || string.IsNullOrEmpty(categoryB))
            .OrderBy(r => r.level);

        var onePageOfProducts = items.ToPagedList(pageNumber, 10); // will only contain 25 products max because of the pageSize

        if (!string.IsNullOrEmpty(query))
        {
            ViewBag.CurrenetFilter = query;
        }

        //delete

        if (!string.IsNullOrEmpty(categoryB))
        {
            ViewBag.Catb = categoryB;
        }

        ViewBag.OnePageOfProducts = onePageOfProducts;
        return View(items.ToPagedList(pageNumber, 10));
    }

Lastly, I've also tried removing the ajax reference and adding the following to the button:

<button class="btn btn-default form-inline" type="submit" onclick="window.location.href=@Url.Action("Search", "items",new {q=@ViewBag.CurrenetFilter})">Search</button>

Nothing happens when I do this.

Yecats
  • 1,715
  • 5
  • 26
  • 40
  • Have you seen this: http://stackoverflow.com/questions/9391201/ajax-beginform-that-can-redirect-to-a-new-page ? I think you are going to have to perform the redirect client-side with `window.location` – David Tansey May 11 '14 at 03:59
  • @DavidTansey Yes - I've seen that page. It doesn't seem to work for me either. I put the onSuccess variable/function within a script page that is loaded ad the end of the page. (I've confirmed with the debugging that is is there.) Changed the method in the Home Controller to say "return Json(new { url = Url.Action("Search", "Items", new { query = q, pageNum = page, categoryB = catb }) });". It never calls the Search action on the items controller. – Yecats May 11 '14 at 04:14
  • It seems like the unobtrusive ajax is preventing the onsuccess call. When I removed it from the layout page it did the proper postback. Is there a way to make them play together? I want to render partial views in the future. – Yecats May 11 '14 at 04:19
  • When I'm in that situation I use the browser debugger to examine the ajax call and see what what sent.. Then I adjust from the there. In many cases it is not that the ajax call is not being made -- it is that the ajax call does not end up matching a route on the server side. – David Tansey May 11 '14 at 04:19
  • Good find. That's probably it. It can be a small pain to get it set up right -- but once it is ok then you're good to go. Just off the top of my head it can get hosed by things like getting loading twice, loading older versions simultaneously (those ones have different names making them look like they belong). You wont have any trouble finding others that have worked through this issue. It is common. – David Tansey May 11 '14 at 04:23
  • I am stumped! I did a search across my entire code base and I only have the library referenced once (and it only one version exists.) I can't seem to find anyone who has a similar problem. (Well, the posts I read basically say to reference the file and make sure that its turned on within the web.config.) – Yecats May 11 '14 at 04:55

0 Answers0