用于搜索表单的ASP.NET MVC分页 [英] ASP.NET MVC Paging for a search form

查看:98
本文介绍了用于搜索表单的ASP.NET MVC分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在MVC中阅读了几个不同的帖子,但没有描述一个场景,我有一个像搜索表单的东西,然后想要在用户的表单下显示搜索条件(带分页)的结果,一旦用户点击提交。



我的问题是,我使用的分页解决方案将创建< a href =...>将通过所需页面的链接,如下所示: http://mysite.com/search/2/而且这一切都很好,很好,我没有将查询的结果发送到内存中的数据库或任何东西,所以我需要再次查询数据库。



如果结果由/ Search的POST控制器操作处理,并且数据的第一页被渲染为如此,当用户单击时,如何获得相同的结果(基于用户指定的表单条件)转到第2页?



有些JavaScript voodoo?杠杆会议状态?使我的GET控制器动作具有与搜索条件(但可选)相同的变量,当调用GET动作时,实例化一个FormCollection实例,将其填充并传递给POST操作方法(那里通过满足DRY)?



有人可以指出我在这种情况下的正确方向还是提供过去已经实现的例子?谢谢!

解决方案

我的方法是使用一个Action来处理帖子和get方案。



这是我可以通过GET和POST方法处理的:

  public ViewResult Index ([DefaultValue(1)] int page,
[DefaultValue(30)] int pageSize,
string search,
[DefaultValue(0)] int regionId,
[DefaultValue 0)] int eventTypeId,
DateTime?from,
DateTime?to)
{
var events = EventRepo.GetFilteredEvents(page,pageSize,search,regionId,eventTypeId,from,至);
var eventFilterForm = EventService.GetEventFilterForm(from,to);

var eventIndexModel = new EventIndexModel(events,eventFilterForm);

return View(Index,eventIndexModel);
}

eventFilterForm 一个演示文稿模型,其中包含我的搜索表单的一些 IEnumerable< SelectListItem> 属性。



eventIndexModel 是一个演示模型,它结合了 eventFilterForm 和搜索结果 - 事件



事件是一种特殊类型的 IPagedList 。您可以获得这里的更多信息和代码 here 。第一个链接介绍了IPagedList,其中第二个链接具有您需要的高级分页方案。



高级分页具有以下使用的方法:

  public static string Pager(this HtmlHelper htmlHelper,int pageSize,int currentPage,int totalItemCount,RouteValueDictionary valuesDictionary)

我使用如下:

 <%= Html.Pager(Model.Events.PageSize,
Model.Events.PageNumber,
Model.Events.TotalItemCount,
new
{
action =index,
controller =search,
search = ViewData.EvalWithModelState(Search),
regionId = ViewData.EvalWithModelState(RegionId),
eventTypeId = ViewData.EvalWithModelState(EventTypeId),
from = ViewData.EvalDateWith ModelState(From),
to = ViewData.EvalDateWithModelState(To)
})%>

创建的链接如下:


/ event / search?regionId = 4& eventTypeId = 39&从= 2009/09/01& to = 2010/08/31& page = 3


<

HTHs,

Charles



Ps。 EvalWithModelState 在下面:



PPs。如果要将日期放在get变量中 - 我建议阅读我的博客帖子 ...: - )

  ///< summary> 
///将从ViewData获取指定的键。它将首先查看ModelState
///,如果在那里找不到,它将调用ViewData.Eval(string key)
///< / summary>
///< param name =viewData> ViewDataDictionary对象< / param>
///< param name =key>搜索字典的键< / param>
///< returns> ModelState中的值,如果它找到一个或调用ViewData.Eval()< / returns>
public static string EvalWithModelState(此ViewDataDictionary视图数据,字符串键)
{
if(viewData.ModelState.ContainsKey(key))
返回viewData.ModelState [key] .Value。 AttemptedValue;

return(viewData.Eval(key)!= null)? viewData.Eval(key).ToString():string.Empty;
}


I've read several different posts on paging w/ in MVC but none describe a scenario where I have something like a search form and then want to display the results of the search criteria (with paging) beneath the form once the user clicks submit.

My problem is that, the paging solution I'm using will create <a href="..."> links that will pass the desired page like so: http://mysite.com/search/2/ and while that's all fine and dandy, I don't have the results of the query being sent to the db in memory or anything so I need to query the DB again.

If the results are handled by the POST controller action for /Search and the first page of the data is rendered as such, how do I get the same results (based on the form criteria specified by the user) when the user clicks to move to page 2?

Some javascript voodoo? Leverage Session State? Make my GET controller action have the same variables expected by the search criteria (but optional), when the GET action is called, instantiate a FormCollection instance, populate it and pass it to the POST action method (there-by satisfying DRY)?

Can someone point me in the right direction for this scenario or provide examples that have been implemented in the past? Thanks!

解决方案

My method is to have an Action that handles both the post and the get scenarios.

This is my which can be handled by both GET and POST methods:

public ViewResult Index([DefaultValue(1)] int page,
                        [DefaultValue(30)] int pageSize,
                        string search,
                        [DefaultValue(0)] int regionId,
                        [DefaultValue(0)] int eventTypeId,
                        DateTime? from,
                        DateTime? to)
{
    var events = EventRepo.GetFilteredEvents(page, pageSize, search, regionId, eventTypeId, from, to);
    var eventFilterForm = EventService.GetEventFilterForm(from, to);

    var eventIndexModel = new EventIndexModel(events, eventFilterForm);

    return View("Index", eventIndexModel);
}

The eventFilterForm is a presentation model that contains some IEnumerable<SelectListItem> properties for my search form.

The eventIndexModel is a presentation model that combines the eventFilterForm and the results of the search - events

The events is a special type of IPagedList. You can get more information and code for that here and here. The first link talks about IPagedList where as the second link has an Advanced Paging scenario which you should need.

The advanced paging has the following method that I use:

public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)

And I use it like so:

<%= Html.Pager(Model.Events.PageSize,
               Model.Events.PageNumber,
               Model.Events.TotalItemCount,
               new
               {
                   action = "index",
                   controller = "search",
                   search = ViewData.EvalWithModelState("Search"),
                   regionId = ViewData.EvalWithModelState("RegionId"),
                   eventTypeId = ViewData.EvalWithModelState("EventTypeId"),
                   from = ViewData.EvalDateWithModelState("From"),
                   to = ViewData.EvalDateWithModelState("To")
               }) %>

This creates links that look like:

/event/search?regionId=4&eventTypeId=39&from=2009/09/01&to=2010/08/31&page=3

HTHs,
Charles

Ps. EvalWithModelState is below:

PPs. If you are going to put dates into get variables - I would recommend reading my blog post on it... :-)

/// <summary>
/// Will get the specified key from ViewData. It will first look in ModelState
/// and if it's not found in there, it'll call ViewData.Eval(string key)
/// </summary>
/// <param name="viewData">ViewDataDictionary object</param>
/// <param name="key">Key to search the dictionary</param>
/// <returns>Value in ModelState if it finds one or calls ViewData.Eval()</returns>
public static string EvalWithModelState(this ViewDataDictionary viewData, string key)
{
    if (viewData.ModelState.ContainsKey(key))
        return viewData.ModelState[key].Value.AttemptedValue;

    return (viewData.Eval(key) != null) ? viewData.Eval(key).ToString() : string.Empty;
}

这篇关于用于搜索表单的ASP.NET MVC分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆