在搜索结果页面中保留表单发布(视图模型)结果的最佳方法是什么?
我有一个包含复选框的搜索表单。该表单是使用如下视图模型构建的
public class SearchViewModel
{
public string Name { get; set; }
public string[] Colors { get; set; }
}
当此视图模型被发回时,我使用这些值来构建查询(使用 EF)。结果将转换为 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));
}
}
现在结果已显示,我想使用 Html.PagedListPager 和 RouteValueDictionary 来创建分页。
@Html.PagedListPager((IPagedList)Model, page => Url.Action("Results", new RouteValueDictionary(ViewBag.SearchViewModel)))
然而;创建的 URL 如下所示:
http://localhost:5139/search?Name=test&Colors=System.String[]&PageIndex=0
颜色的值最终是类型而不是值。我希望 URL 看起来更像:
http://localhost:5139/search?Name=test&Colors=[Blue,Pink,Yellow]&PageIndex=0
- 在搜索结果页面中保留表单发布(视图模型)结果的最佳方法是什么?
- RouteValueDictionary 可以支持复杂对象吗?
- 我应该使用类似的东西unbinder http://mhinze.com/2010/03/17/serializing-models-for-routevaluedictionary-and-later-model-binding/
- 使用 ViewData 还是 Session 会更好吗?