I have a products page with a search box. When a search is submitted there's a redirection to a search results page.
If another search is submitted then the query string
remains intact (results are populated via ajax
).
So what I've done is running twice to the server, one time using ajax
to set the Session
value and another for the redirect itself:
Products page
function search() {
//get the search query text
var query = $('#productSearchInput').val();
//url to a method on the search controller
var url = 'http://SearchPageUrl' + '/SetSearchQuery';
//set the session value
$.get(url, { query: query })
.done(function () {
//actual redirect
window.location.href = 'http://SearchPageUrl';
});
}
Search results controller - set the session value
public void SetSearchQuery(string query)
{
System.Web.HttpContext.Current.Session["searchQuery"] = WebUtility.HtmlDecode(query);
}
Search page entry point
public ActionResult Index()
{
string query = System.Web.HttpContext.Current.Session["searchQuery"].ToString();
ViewBag.query = query;
//get the products...
//var results = GetProducts(query);
return View("~/Views/something/SearchResults.cshtml", results);
}
So this works but it feels like a lot of not so maintainable or efficient implementation.
How can I make it better?