开发者

Save State of Search

I have a web page that builds a table of search results. the user can then edit those items. When the user is returned to the page it returns to it's unsearched state (as it is designed).

I want to have them return to the query. how can i save that page state?

@model List<MyNamespace.Models.MyModel>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Ajax.BeginForm("Search", "MyController",
    new AjaxOptions
    {
        HttpMethod = "GET",
        InsertionMode = InsertionMode.Replace,
        UpdateTargetId = "searchResult开发者_Go百科s"
    }))
{
    <input type="text" name="q" data-autocomplete="@Url.Action("SearchProviders", "MyController")" />
    <input type="submit" value="Search" />
}

<div id="searchResults" />

Controller Action:

public PartialViewResult Search(string q)
        {
            var jobs = MyRepository.GetJobsByQuery(String.Format("DataProvider='{0}'", q));
            return PartialView("_MyPartialRowset", jobs);
        }


I'd suggest having your search form submit using HTTP GET; the search parameters/terms could be included in the query string of the search results page, like this:

/Search?q=cthulhu

Then, make all of your edit links so that they have a returnUrl in the query string:

/Things/235/Edit?returnUrl=%2FSearch%3Fq%3Dcthulhu

When the save or cancel completes, return to the page specified in returnUrl. That way, no matter where the edit was opened from, you can use the same logic to return to the previous page.

As part of this, I'd also make the search results page is not cached.

Update:

If your search is done through AJAX, then the easiest way for you to preserve the search parameters to auto-populate when the user comes back is to place the search parameters in a cookie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜