What is the best method to retain the results of a form post (view model) across search results page?
I have a search form that contains checkboxes. This form is build up using a view model like
public class SearchViewModel
{
public string Name { get; set; }
public string[] Colors { get; set; }
}
When this view model gets posted back I use the values to build a query (using EF). The results are turned into a PagedList.
public class SearchController : Controller
{
public ActionResult Index()
{
//this displays the search form.
return View();
}
public ActionResult Results(string game, SearchViewModel vm)
{
//this displays the results
ViewBag.SearchViewModel = vm;
var matches = _repository.AsQueryable()
.ColorOr(vm.Colors)
.WhereIf(vm.Name.IsNotEmpty(), x => x.Name.Contains(vm.Name.Trim()));
return View(matches.ToPagedList(1, 10));
}
}
Now that the results are displayed I would like to use Html.PagedListPager and RouteValueDictionary to create paging.
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Results", new RouteValueDictionary(ViewBag.SearchViewModel)))
However; the URL created looks like this:
http://localhost:5139/search?Name=test&Colors=System.String[]&PageIndex=0
The values for Colors ends up being the type not the values. I was hoping the URL looks more like:
http://localhost:5139/search?Name=test&Colors=[Blue,Pink,Yellow]&PageIndex=0
- What is the best method to retain the results of a form post (view model) across search results page?
- Can RouteValueDictionary support complex objects?
- Should I use something like unbinder
- Would I be better off using ViewData or Session?