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.