跨搜索結果頁面保留表單發佈(視圖模型)結果的最佳方法是什麼?ASP.NET MVC RouteValueDictionary和複雜對象
我有一個包含複選框的搜索表單。這種形式是建立使用視圖模型像
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
Colors的值最終是類型而不是值。我希望的URL看起來更像:
http://localhost:5139/search?Name=test&Colors=[Blue,Pink,Yellow]&PageIndex=0
- 什麼能夠保留整個搜索結果頁面表單POST(視圖模型)的結果,最好的方法是什麼?
- RouteValueDictionary是否支持複雜對象?
- 我應該使用類似unbinder
- 我會更好使用ViewData或會話嗎?
哇。這是我確切的問題。 +1 – 2013-03-08 15:01:48