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

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

问题描述

我读过关于分页几个不同岗位瓦特/ MVC中,但没有描述一个场景,我有一些像一个搜索表单,然后想显示的搜索标准的形式,一旦用户下的结果(分页)点击提交。

我的问题是,我使用将创造&LT寻呼解决方案; A HREF =...>链接,将通过所需页面,如下所示: http://mysite.com/search/2/ 虽然这是所有罚款和花花公子,我没有查询结果发送到数据库中存储或任何东西,所以我需要重新查询数据库。

如果结果由/搜索和数据的第一页的POST控制器动作处理呈现为这样,我怎么得到相同的结果(根据用户指定的格式标准),用户点击时,移动到第2页?

一些JavaScript巫术?利用会话状态?让我GET控制器操作必须通过搜索条件(但可选),当GET动作称为预计相同的变量,实例的FormCollection的实例,填充它,然后(通过有-满足DRY)将它传递给POST操作方法<? / p>

有人能指出我在正确的方向对于这种情况,或提供已在过去实施的例子吗?谢谢!


解决方案

我的方法是具有同时处理岗位和获得场景的动作。

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

 公开的ViewResult指数([默认值(1)] INT页,
                        [默认值(30)] INT pageSize的,
                        字符串搜索,
                        [默认值(0)] INT regionId,
                        [默认值(0)] INT eventTypeId,
                        约会时间?从,
                        约会时间?至)
{
    VAR的事件= EventRepo.GetFilteredEvents(页,pageSize的,搜索,regionId,eventTypeId,从,到);
    VAR eventFilterForm = EventService.GetEventFilterForm(从,到);    VAR eventIndexModel =新EventIndexModel(事件,eventFilterForm);    返回视图(指数,eventIndexModel);
}

eventFilterForm 是包含一些的IEnumerable℃的presentation模型;对我的搜索表单性能; SelectListItem&GT

eventIndexModel 为presentation模型,结合了 eventFilterForm 和搜索结果 - 事件

事件是一种特殊类型的 IPagedList 的。你可以得到更多的信息和code为 这里。有关IPagedList第一个环节在哪里谈的第二个链接有一个高级分页的情况,你应该需要。

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

 公共静态字符串寻呼机(此的HtmlHelper的HtmlHelper,诠释的pageSize,诠释当前页,诠释totalItemCount,RouteValueDictionary values​​Dictionary)

和我使用它像这样:

 &LT;%= Html.Pager(Model.Events.PageSize,
               Model.Events.PageNumber,
               Model.Events.TotalItemCount,
               新
               {
                   行动=指数,
                   控制器=搜索,
                   搜索= ViewData.EvalWithModelState(搜索),
                   regionId = ViewData.EvalWithModelState(RegionId),
                   eventTypeId = ViewData.EvalWithModelState(EventTypeId),
                   从= ViewData.EvalDateWithModelState(发件人),
                   为= ViewData.EvalDateWithModelState(到)
               })%GT;

这将创建一个看起来像链接:


  

/事件/搜索regionId = 4和; eventTypeId = 39安培;从= 2009/09/01安培;为= 2010/08/31安培;页= 3


HTHS,结果
查尔斯

诗。 EvalWithModelState 是如下:

的PP。如果你打算把日期,GET变量 - 我建议你阅读的就可以了......我的博客文章: - )

  ///&LT;总结&gt;
///将得到的ViewData指定的键。它会先看看在的ModelState
///如果它不是在那里找到,它会调用ViewData.Eval(字符串键)
///&LT; /总结&gt;
///&LT; PARAM NAME =可视数据&GT;的ViewDataDictionary对象&LT; /参数&GT;
///&LT; PARAM NAME =键&GT;关键搜索字典&LT; /参数&GT;
///&LT;返回&gt;在ModelState中值,如果它找到一个或调用ViewData.Eval()&LT; /回报&GT;
公共静态字符串EvalWithModelState(此的ViewDataDictionary可视数据,字符串键)
{
    如果(viewData.ModelState.ContainsKey(键))
        返回viewData.ModelState [关键] .Value.AttemptedValue;    返回(viewData.Eval(键)!= NULL)? viewData.Eval(密钥)的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天全站免登陆